Spring Boot JPA DataIntegrityViolationException

huangapple go评论43阅读模式
英文:

Spring boot JPA DataIntegrityViolationException

问题

我有两个实体类,分别命名为ProductComboProduct。 Product包含有关特定产品的所有信息,并具有主键productId。 ComboProduct保存一个与Product具有@OneToMany关系的产品列表,并具有名为comboProductId的主键。当我创建多个具有相同产品集的ComboProduct实例时,会出现以下DataIntegrityViolationException



      public class Product {
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Long productId;
    	@OneToOne
    	private MasterProduct masterProduct;
    	@NotBlank
    	private String productName;
    	private String productDescription;
    	
    	private Double productSellingPrice; // mrp
    	private Double productOfferPrice;   // price at which the user sells
    	private Double productPurchasePrice;
    	private float totalTaxPercentage;
    	
    	private float productMargin;
    	
    	@OneToOne
    	private MerchantStore store;
    	
    	
    	@ManyToOne 
    	private ComboProduct comboProduct;       
      }

      @Entity
      public class ComboProduct {
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Long comboProductId;
    	private String comboProductKey;
    	@OneToMany
    	private List<Product> products;
    	@NotBlank
    	private String comboName;
    	private String comboDescription;
    }
   
org.springframework.dao.DataIntegrityViolationException: 
无法执行语句;SQL [n/a];约束 [UK_nm4dyaqp2f780nx73vq9abbw3];
嵌套异常是org.hibernate.exception.ConstraintViolationException: 
无法执行语句

如何解决这个问题?

英文:

I have two entity classes, named Product and ComboProduct. Product contains all the information about a particular product and has a Primary Key productId. ComboProduct holds a list with a @OneToMany relationship to the Product and has a primary key named comboProductId. When I create multiple Comboproduct instances with the same set of products, I get the following DataIntegrityViolationException:

  

      public class Product {
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Long productId;
    	@OneToOne
    	private MasterProduct masterProduct;
    	@NotBlank
    	private String productName;
    	private String productDescription;
    	
    	private Double productSellingPrice; // mrp
    	private Double productOfferPrice;   // price at which the user sells
    	private Double productPurchasePrice;
    	private float totalTaxPercentage;
    	
    	private float productMargin;
    	
    	@OneToOne
    	private MerchantStore store;
    	
    	
    	@ManyToOne 
    	private ComboProduct comboProduct;       @Entity
        public class ComboProduct {
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Long comboProductId;
    	private String comboProductKey;
    	@OneToMany
    	private List<Product> products;
    	@NotBlank
    	private String comboName;
    	private String comboDescription;
    
   
org.springframework.dao.DataIntegrityViolationException: 
could not execute statement; SQL [n/a]; 
constraint [UK_nm4dyaqp2f780nx73vq9abbw3]; 
nested exception is org.hibernate.exception.ConstraintViolationException: 
could not execute statement\n\t
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:298)\n\t
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)\n\t
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:538)\n\t
at org.springframework.transaction.support.


How can I resolve this?

</details>


# 答案1
**得分**: 0

由于您已经建模了一对多关系,您定义了一个组合与多个产品相关联。但产品只能分配给一个组合。

如果您希望产品能够属于多个组合,您需要建模多对多关系。当然,这也意味着需要在数据库上有一个相应的映射表。

<details>
<summary>英文:</summary>

as you have modeled a One-to-Many relationship, you defined that one combo is associated to multiple products. But products can only be assigned to one combo. 

If you want products to be able to belong to multiple combos, you would need to model a Many-to-Many relationship. That would of course also imply to have a respective mapping table on the database.

</details>



# 答案2
**得分**: 0

2个实体类之间的关系没有正确建立。

```java
@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long productId;

    @ManyToOne
    private ComboProduct comboProduct;
}

@Entity
public class ComboProduct {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long comboProductId;
    
    @OneToMany
    private List<Product> products;
}
英文:

Relationships between the 2 entity classes were not established properly.
`

@Entity 
public class Product {
@Id 
@GeneratedValue    (strategy = GenerationType.IDENTITY)
private Long productId;

@ManyToOne 
private ComboProduct comboProduct; }


@Entity 
public class ComboProduct {     
@Id     
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long comboProductId;      
@OneToMany     
private List&lt;Product&gt; products; 
} `

答案3

得分: 0

问题是您需要告诉Hibernate,OneToMany映射将使用另一张表上的外键,否则它会假定列表中的实体必须具有唯一的Id。

为了实现这一点,您必须在拥有映射的属性上添加@JoinColumn注解(类ComboProduct属性products)。
请尝试以下方式:

@Entity
public class ComboProduct {
    ...
    @OneToMany(mappedBy = "comboProduct")
    private List<Product> products;
}
@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long productId;
    private String productDescription;
    private Double productSellingPrice;
    private Double productOfferPrice;
    @ManyToOne
    @JoinColumn
    private ComboProduct comboProduct;
}
英文:

The problem is you need to tell Hibernate that the OneToMany mapping will use a foreign key on another table, otherwise, it will assume that the entity in the list must have a unique Id.

To do so, you must add @JoinColumn on the attribute owning the mapping (class ComboProduct attribute products).
Try the following:

@Entity
public class ComboProduct {
    ...
    @OneToMany(mappedBy = &quot;comboProduct&quot;)
    private List&lt;Product&gt; products;
@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long productId;
    private String productDescription;
    private Double productSellingPrice;
    private Double productOfferPrice;
    @ManyToOne
    @JoinColumn
    private ComboProduct comboProduct;
}

huangapple
  • 本文由 发表于 2020年7月30日 04:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63161938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定