英文:
Hibernate (Spring JPA): @ManyToOne JoinColumns (which are an EmbeddedId) are null
问题
我没有看到我的错误,在stackoverflow和Google上进行了研究后,我认为代码应该是正确的。但是Hibernate(spring-boot-starter-data-jpa 2.2.4)仍然将JoinColumns填充为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
。
有什么想法吗?
英文:
I didn't see my error and after a research on stackoverflow and Google I think the code should be correct. But Hibernate (spring-boot-starter-data-jpa 2.2.4) still fill the JoinColumns with null
.
Here is my OneToMany
class:
@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
}
My EmbeddedId
class:
@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
}
And finally my ManyToOne
class:
@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
}
As you may see I want to use the attributes (id
, iddate
and idint
) as a combined primaray key in both tables.
When I create the TableAEntity
object I add several TableBEntity
objects to the tableBentries
attribute. And for every TableBEntity
I set the reference to the TableAEntity
(attribute tableA
). And of course the MyId
(attribute id
) object is filled.
After saveing the TableAEntity
object, Hibernate also stores all TableBEntity
but the fields id
, iddate
and idint
(all JoinColumn's) are null
.
Any idea?
答案1
得分: 1
似乎是我在 TableBEntity
中的 @Id
导致了这些问题。如果我将它移到另一个属性,它就正常工作。
英文:
It seems that @Id
in my TableBEntity
causes the problems. If I moved it to another attribute, it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论