假设我们有一个 @OneToMany 关系:
@Entity
pulic class MyEntity {
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private List<MyOtherEntity> myOtherEntityList;
}
当我从数据库加载 MyEntity
实例时,List
中 MyOtherEntity
实例的顺序是什么?我可以指定我希望它们的顺序吗,例如,如果我希望它们按特定字段排序?
最佳答案
要订购列表,您可以使用两种策略
第一
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@OrderBy("name ASC")
private List<MyOtherEntity> myOtherEntityList;
Specifies the ordering of the elements of a collection valued association or element collection at the point when the association or collection is retrieved.
第二个
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@OrderColumn("order")
private List<MyOtherEntity> myOtherEntityList;
Specifies a column that is used to maintain the persistent order of a list
请记住,如果您不使用上述注释,您的 JPA 提供程序将执行简单的选择,并且您会丢失任何排序。
关于jpa - 在 @OneToMany 关系中,多方的实体将以什么顺序获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774686/