英文:
Spring boot JPA DataIntegrityViolationException
问题
我有两个实体类,分别命名为Product和ComboProduct。 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<Product> 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 = "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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论