JPA @OrderColumn没有订购列表

时间:2014-08-07 13:07:32

标签: hibernate jpa orm in-memory-database

我在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;

谢谢!

1 个答案:

答案 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;

摘自this page:

  

在JPA中,ManyToOne关系总是(几乎总是)   需要定义OneToMany关系,ManyToOne总是如此   定义外键(JoinColumn),OneToMany必须使用a   mappedBy来定义它的逆ManyToOne。

查看此页