我有2个实体(列表)与另一个带有代码的实体相关,我应该如何定义JPA注释,如果我想单独保存ProductCharacteristic和ProductStatistic列表但是在表中有相同的产品?
我是否需要创建一个包含Product和ProductCharacteristic列表的ProductCharacteristicTable实体?
目前,我有以下错误:引起:org.hibernate.TransientPropertyValueException:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.xyz.ProductCharacteristic.product - > com.xyz.Product
或此错误 由以下原因引起:javax.persistence.EntityExistsException:具有相同标识符值的另一个对象已与会话相关联
@Entity
public class Product {
@Id
private String code;
}
@Entity
public class ProductCharacteristic {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
private Product product;
private String attributeCode;
private String attributeValue;
}
@Entity
public class ProductStatistic {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Product product;
private String attributeCode;
private String attributeFormat;
private String attributeValue;
}
感谢您的帮助