我使用 postgres 数据库,我有一个 Conventionnement
实体,它与约定和组织有 ManyToOne
关系,我想创建 3 个主键 convention_id
、organization_id
和 GeneratedValue id
,我像下面的示例一样使用了 @Embeddable,但出现了以下错误
Caused by: org.hibernate.annotations.common.AssertionFailure: Declaring class is not found in the inheritance state hierarchy: ConventionnementIdentity
当我在 Conventionnement 类中移动 id 时出现此错误
ConventionnementIdentity must not have @Id properties when used as an @EmbeddedId
@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
}
最佳答案
您已经定义了一个 @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
}
关于java - 具有 @EmbeddedId 、 @GeneratedValue 和 @ManyToOne 的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465335/