有人可以帮助我正确地映射这3个实体,因为我尝试了许多解决方案但无法正常工作。 我有这3个实体: 产品,物品和包装应按以下方式映射:
public class Product implements Serializable {
@OneToMany(mappedBy="parentProduct", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@PrimaryKeyJoinColumn
private List<Pack> packListParent;
@OneToMany(mappedBy="childProduct", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@PrimaryKeyJoinColumn
private List<Pack> packListChild;
}
public class Pack implements Serializable {
@EmbeddedId
protected PackPK PackPK;
@MapsId("itemId")
@JoinColumn(name = "item_id", referencedColumnName = "item_id", insertable = true, updatable = true)
@ManyToOne(fetch = FetchType.LAZY)
private Item item;
@MapsId("parentProductId")
@JoinColumn(name = "product_id", referencedColumnName = "product_id", insertable = true, updatable = true)
@ManyToOne(fetch = FetchType.LAZY)
private Product parentProduct;
@MapsId("childProductId")
@JoinColumn(name = "child_product_id", referencedColumnName = "product_id", insertable = true, updatable = true)
@ManyToOne(fetch = FetchType.LAZY)
private Product childProduct;
}
@Embeddable
public class PackPK implements Serializable {
@Basic(optional = false)
@Column(name = "product_id")
private Long parentProductId;
@Basic(optional = false)
@Column(name = "item_id")
private Long itemId;
@Basic(optional = false)
@Column(name = "child_product_id")
private Long childProductId;
}
public class Item implements Serializable {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@PrimaryKeyJoinColumn
private List<Pack> PackList;
}
此映射在eclipselink中有效,但是当我使用eclipselink迁移到spring数据jpa时,启动服务器应用程序时会出现此警告。
这是错误消息:
WARN - [AnnotationConfigServletWebServerApplicationContext]
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'produitService': Unsatisfied dependency expressed through field 'PackService';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'PackService': Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException: Expected id attribute type [class com.project.jpa.domain.PackPK]
on the existing id attribute [SingularAttributeImpl[EntityTypeImpl@1851998201:Item
[ javaType: class com.project.jpa.domain.Item descriptor: RelationalDescriptor(com.project.jpa.domain.Item --> [DatabaseTable(item)]), mappings: 47],
org.eclipse.persistence.mappings.ManyToOneMapping[item]]] on the identifiable type [EntityTypeImpl@1341697553:Pack [ javaType: class com.project.jpa.domain.Pack descriptor:
RelationalDescriptor(com.project.jpa.domain.Pack --> [DatabaseTable(pack_svod_content)]), mappings: 6]] but found attribute type [class com.project.jpa.domain.Item].