Java 我如何确定是否使用Spring jpa插入或更新记录

Java 我如何确定是否使用Spring jpa插入或更新记录,java,mysql,spring-boot,spring-data-jpa,Java,Mysql,Spring Boot,Spring Data Jpa,在我的spring库存项目中,我想在包含以下字段的Item表中插入项目 名称 颜色 描述 量 买价 售价。 现在我在请求中发送一个json { "name":"iphone 5s cover", "buyingPrice": 300, "color":"white", "description":"Hard cover for iphone", "sellingPrice":400, "quantity":30 } 这个东西会在我通过自动递增添

在我的spring库存项目中,我想在包含以下字段的Item表中插入项目

名称 颜色 描述 量 买价 售价。 现在我在请求中发送一个json

{
    "name":"iphone 5s cover",
    "buyingPrice": 300,
    "color":"white",
    "description":"Hard cover for iphone",
    "sellingPrice":400,
    "quantity":30
 }
这个东西会在我通过自动递增添加Id的表中插入新记录。 现在,当我要添加具有不同数量的相同项目时,它应该更新数量,而不是添加新记录

我现在可以做的是检查请求的每一个内容,如果它存在于它发送Id的表中,或者更新该特定Id处的记录

或者,在spring引导中,还有其他更好的方法可以识别自己来更新此记录。 或者我能做些什么来处理这种情况

尝试使用@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作为主要的,名字和颜色作为复合的。 这对实现这一目标有意义吗。
感激之情:

您需要首先通过其id获取物品,如下所示

Item item = itemRepository.findById(YOUR_ID);
然后你需要像这样更新它的状态

item.setQuantity(YOUR_QUANTITY);
然后最后像这样更新数据库中的项

item = itemRepository.save(item); //Now the item object contains the updated quantity.

我相信JPA会在save方法上自动做到这一点。它的行为类似于创建或更新。重要的部分是ID。如果要更新,还需要将正确的ID传递到对象模型的save方法中。这是一个问题,它会自动递增以获取ID。我必须根据颜色和名称获取对象。有没有更好的方法。听起来你想让color+name成为一个唯一的键呢?同样的想法是把它们组合成一个复合键。要创建一个可嵌入的田园诗,你需要把它作为你的键。如果颜色和名称已经定义了唯一的对象,那么您就不需要自动生成ID。我没有ID,因为我不知道它是否在之前插入。必须有某个键唯一标识您的项目实体!!我现在已经使用id类注释定义了复合键的颜色和名称,但它会引发我和映射类属性A_项的异常,而在实体apst-your实体中也找不到。看起来bug就在那里。异常日志显示您的销售实体类中没有属性\u com\u was\u inventory\u model\u Sale\u items!
item = itemRepository.save(item); //Now the item object contains the updated quantity.