我在Child表中定义了一个“Ordinal”INT列。出于某种原因,自动排序没有生效。我正在使用内存数据库的集成测试对此进行测试。也许这很重要?
public class ParentEntity
...
@OneToMany
@OrderColumn(name = "Ordinal")
@JoinColumn(name = "ParentId", nullable = false)
private List<ChildEntity> childEntities = new ArrayList<>();
public class ChildEntity
...
@ManyToOne
@JoinColumn(name = "ParentId", nullable=false, insertable=false, updatable=false)
private ParentEntity parent;
谢谢!
答案 0 :(得分:1)
问题是您的某个实体应该是owner of the bidirectional (ManyToOne) relationship:
public class ParentEntity
...
@OneToMany(mappedBy="parent")//this mappedBy tells that ChildEntity is the owner of the entity
@OrderColumn(name = "Ordinal")
@JoinColumn(name = "ParentId", nullable = false)
private List<ChildEntity> childEntities = new ArrayList<>();
public class ChildEntity
...
@ManyToOne
@JoinColumn(name = "ParentId", nullable=false, insertable=false, updatable=false)
private ParentEntity parent;
在JPA中,ManyToOne关系总是(几乎总是) 需要定义OneToMany关系,ManyToOne总是如此 定义外键(JoinColumn),OneToMany必须使用a mappedBy来定义它的逆ManyToOne。
查看此页