我的数据库结构如下:
Table : product
prod_id
...
Table : product_category
prod_id
category_id
...
Product_category 对 (prod_id,category_id) 有唯一约束
我的JPA文件如下:
@Entity
@Table(name = "product")
public class ProductEntity implements Serializable {
...
@OneToMany(mappedBy = "product")
private List<ProductCategoryEntity> productCategories = new ArrayList<>();
...
}
@Embeddable
public class ProductCategoryPK implements Serializable {
@Column(name = "prod_id")
private ProductEntity product; //Compiler error : 'Basic' attribute type should not be 'Persistence Entity'
@Column(name = "category_id")
private Integer categoryId;
...
}
@Entity
@Table(name = "product_category")
public class ProductCategoryEntity implements Serializable {
@EmbeddedId
private ProductCategoryPK key;
@ManyToOne
@MapsId("prod_id")
private ProductEntity product;
...
}
在 ProductCategoryPK....行:私有 ProductEntity 产品;
我在编辑器中收到以下错误“编译器错误:‘基本’属性类型不应为‘持久性实体’”
不知道如何映射它。非常感谢帮助
您应该使用基本类型来保存产品标识符,而不是将产品表示为对 ProductEntity 类的引用。
例如,如果产品标识符是数字,您可以将其更改为 Integer 类型:
@Column(name = "prod_id")
private Integer productId;