我没有看到我的错误,在对 stackoverflow 和 Google 进行研究后,我认为代码应该是正确的。但是 Hibernate (spring-boot-starter-data-jpa 2.2.4) 仍然用null
.
这是我的OneToMany
课:
@Entity
@Table(name = "tablea", schema = "")
public class TableAEntity implements Serializable {
private static final long serialVersionUID = 7890327260188587351L;
@EmbeddedId
private MyId id;
@OneToMany(cascade = ALL, mappedBy = "tableA", orphanRemoval = true, fetch = FetchType.LAZY)
private List<TableBEntity> tableBentries;
// Getter + Setter
}
我的EmbeddedId
班级:
@Embeddable
public class MyId implements Serializable {
private static final long serialVersionUID = -8267953052238233498L;
@Column(name = "id")
private String id;
@Column(name = "iddate")
private Date iddate;
@Column(name = "idint")
private BigInteger idint;
// Getter + Setter
}
最后是我的ManyToOne
课:
@Entity
@Table(name = "tableB", schema = "")
public class TableBEntity implements Serializable {
private static final long serialVersionUID = -4648090658471459969L;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "id", referencedColumnName = "id"),
@JoinColumn(name = "iddate", referencedColumnName = "iddate"),
@JoinColumn(name = "idint", referencedColumnName = "idint")
})
private TableAEntity tableA;
// Some other attributes
// Getter + Setter
}
如您所见,我想在两个表中使用属性 (id
和iddate
)idint
作为组合的主键。
当我创建TableAEntity
对象时,我将几个TableBEntity
对象添加到tableBentries
属性中。并且对于每个TableBEntity
我设置对TableAEntity
(属性tableA
)的引用。当然,MyId
(属性id
)对象已被填充。
保存对象后TableAEntity
,Hibernate 还会存储除TableBEntity
字段之外的所有字段id
,iddate
并且idint
(所有 JoinColumn 的)是null
。
任何的想法?