我使用postgres数据库,我有ManyToOne
实体与约定和组织有convention_id
关系,我想创建3个主键organization_id
,GeneratedValue id
和@Entity
@Table(name = "conventionnement")
public class Conventionnement implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ConventionnementIdentity conventionnementIdentity;
@MapsId("convention_id")
@ManyToOne
private Convention convention;
@MapsId("organization_id")
@ManyToOne
private Organization organization;
//getter and setter
}
@Embeddable
public class ConventionnementIdentity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "convention_id", insertable = false, updatable = false)
private Long conventionId;
@Column(name = "organization_id", insertable = false, updatable = false)
private Long organizationId;
//getter and setter
}
,我使用@Embeddable,如下例所示,但我有以下错误
引起:org.hibernate.annotations.common.AssertionFailure: 在继承状态层次结构中找不到声明类: ConventionnementIdentity
当我在Conventionnement类中移动id时出现此错误
当用作a时,ConventionnementIdentity不得具有@Id属性 @EmbeddedId
{{1}}
答案 0 :(得分:2)
您已经定义了@EmbeddedId
,但在其中您声明了另一个@Id
,因此错误。将生成的ID指向表格中正确的id列(下面我假设" id"作为列名称):
@Embeddable
public class ConventionnementIdentity implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", sequenceName="YOUR_DB_SEQ", allocationSize=1)
private Long id;
@Column(name = "convention_id", insertable = false, updatable = false)
private Long conventionId;
@Column(name = "organization_id", insertable = false, updatable = false)
private Long organizationId;
//getter and setter
}