我在库存我的春天项目的情况下我想插入其中包含以下字段的项目表格中的项目更新记录。我怎么能确定是否插入或与Spring JPA
- 名称
- 颜色
- 描述
- 量
- buying_price
- selling_price。
现在我在请求
{
"name":"iphone 5s cover",
"buyingPrice": 300,
"color":"white",
"description":"Hard cover for iphone",
"sellingPrice":400,
"quantity":30
}
发送JSON这个东西会插入新记录中,我通过auto-increment
加入标识表。 现在,当我要添加不同的数量相同的项目时,它应该更新数量,而不是添加新记录。
我现在可以做的是检查每个请求的所有内容,如果它存在于它发送Id的表中或更新该特定Id中的记录。
或者还有其他更好的方式在春季启动它会标识自己更新此记录。 或者我能做些什么来处理这种情况。
试图创建使用@ClassId
注释,但得到的例外
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property-ref [_com_was_inventory_model_Sale_items] not found on entity [com.was.inventory.model.Sale]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at com.was.inventory.InventoryApplication.main(InventoryApplication.java:10) [classes/:na]
Caused by: org.hibernate.MappingException: property-ref [_com_was_inventory_model_Sale_items] not found on entity [com.was.inventory.model.Sale]
at org.hibernate.mapping.PersistentClass.getReferencedProperty(PersistentClass.java:413) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$DelayedPropertyReferenceHandlerAnnotationImpl.process(InFlightMetadataCollectorImpl.java:1229) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processPropertyReferences(InFlightMetadataCollectorImpl.java:1938) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1625) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: property [_com_was_inventory_model_Sale_items] not found on entity [com.was.inventory.model.Sale]
at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:469) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.getReferencedProperty(PersistentClass.java:409) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
... 28 common frames omitted
这里一个复合键是实体
@Entity
@Table(name = "Item")
@IdClass(ItemsKey.class)
public class Item implements Serializable {
private Integer id;
private String name;
private Category category;
private String description;
private String color;
private Long sellingPrice;
private Long buyingPrice;
private Integer quantity;
private String pictureLink;
private Set<Sale> sales;
private Set<Orders> orders;
private Set<Purchase> purchases;
public Item() {
}
public Item(String name, Category category, String description, String color, Long sellingPrice, Long buyingPrice, Integer quantity) {
this.name = name;
this.category = category;
this.description = description;
this.color = color;
this.sellingPrice = sellingPrice;
this.buyingPrice = buyingPrice;
this.quantity = quantity;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name = "CategoryId")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Id
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Long getSellingPrice() {
return sellingPrice;
}
public void setSellingPrice(Long sellingPrice) {
this.sellingPrice = sellingPrice;
}
public Long getBuyingPrice() {
return buyingPrice;
}
public void setBuyingPrice(Long buyingPrice) {
this.buyingPrice = buyingPrice;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public String getPictureLink() {
return pictureLink;
}
public void setPictureLink(String pictureLink) {
this.pictureLink = pictureLink;
}
@ManyToMany(mappedBy = "items")
public Set<Sale> getSales() {
return sales;
}
public void setSales(Set<Sale> sales) {
this.sales = sales;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "ItemOrders", joinColumns = @JoinColumn(name = "Item_id",
referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Orders_id", referencedColumnName = "id"))
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
@ManyToMany(mappedBy = "items")
public Set<Purchase> getPurchases() {
return purchases;
}
public void setPurchases(Set<Purchase> purchases) {
this.purchases = purchases;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
这里是ID级别。
public class ItemsKey implements Serializable {
private String name;
private String color;
public ItemsKey() {
}
public ItemsKey(String name, String color) {
this.name = name;
this.color = color;
}
/**
* getters and setters
**/
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "color", nullable = false)
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
这个实体与其他一些其他的关系没有添加复合主键,它工作的很好。
我在哪里错了。 我想要有ID作为主要和名称和颜色作为复合材料。 这是否有意义实现这一目标。 感恩:)
我相信JPA会做到这一点自动保存方法。它表现为创建或更新。重要的部分是身份证。如果您想更新,则还需要将对象模型的正确ID传递给save方法。 – pandaadb
这是问题它的自动增量来获得我必须得到的颜色和名称的基础上的对象的ID。有没有更好的办法。 – Root
这听起来像你想要颜色+名称是一个独特的钥匙呢? – pandaadb