Hibernate 嵌套复合键
我有以下两个实体的组合键Hibernate 嵌套复合键,hibernate,jpa,composite-key,Hibernate,Jpa,Composite Key,我有以下两个实体的组合键 @Embeddable public class ProductId implements Serializable { @Column(name = "Id", columnDefinition = "bigint identity(1,1)") private Long id; @Column(name = "ProductTypeName") private String name; .... } @Embeddable pub
@Embeddable
public class ProductId implements Serializable {
@Column(name = "Id", columnDefinition = "bigint identity(1,1)")
private Long id;
@Column(name = "ProductTypeName")
private String name;
....
}
@Embeddable
public class ApplicationId implements Serializable{
@Column(name = "Id", columnDefinition = "bigint identity(1,1)",
nullable = false, insertable = false, updatable = false)
private Long id;
@Embedded
@AttributeOverride(name="id",
column=@Column(name="ProductId",
columnDefinition = "bigint identity(1,1)",
nullable = false, insertable = false,
updatable = false))
private ProductId productId;
....
}
我有两个不同的实体,都具有上面定义的复合键。我的问题是嵌套复合键,我正试图让它工作,但当hibernate扫描以ApplicationId Embbable对象作为其PK的实体时,它失败了(下面是给出问题的实体的映射)
Hibernate抱怨出现以下错误
,原因是:org.Hibernate.MappingException:在应用程序中找不到逻辑名称为ProductId的列
对于任何有此问题的人,最好使用IdClass方法。此外,我认为这是一种更好的方法,因为参与复合id的所有实体都用@id注释,代码更可读、更容易理解。如果有人需要一个例子,让我知道我会张贴答案。
@Entity(name = "Application")
@Table(name = "Application")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Application {
@EmbeddedId
private ApplicationId applicationPk;
@MapsId("productId")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumns({@JoinColumn(name = "ProductId", referencedColumnName = "Id",
nullable = false, insertable = true,
updatable = true),
@JoinColumn(name = "ProductTypeName",
referencedColumnName = "ProductTypeName",
nullable = false, insertable = true,
updatable = true)})
@ForeignKey(name = "FK_Application_Product")
private Product product;
....}