具有@EmbeddedId,@ GeneratedValue和@ManyToOne的实体

时间:2018-03-24 13:38:05

标签: java hibernate jpa jpa-2.0 composite-key

我使用postgres数据库,我有ManyToOne实体与约定和组织有convention_id关系,我想创建3个主键organization_idGeneratedValue 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}}

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
}