0

@OrderBy在我的 bean 上使用子句,当我从持久层获取这个对象时它工作正常,但是当我尝试使用保存这个数据时

persistedObject = saveAndFlush(MyCustomObject);

结果persistedObject未按照@OrderBy子句指定的方式排序。

代码片段:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "COLLECTION_ID")
@OrderBy("order ASC")
private Set<MySections> sections;
class MySections {
  // Some Properties
    @Column(name = "SEQ_NO")
    private Integer order;
}

存储库相关代码

// this brings sections ordered by order property
collectionRepository.findById("123"); 


// Sections in persistedCollection are not ordered
persistedCollection = collectionRepository.saveAndFlush(collection); 

4

1 回答 1

2

这是因为@OrderBy没有直接在数据库中实现订单。它获取数据并在内存中执行排序。为了实现,你所描述的你必须使用@OrderColumn. 它维护数据库中行的持久顺序。

还有一个建议 - 仅使用select查询不是检查排序的好选择,因为数据库不能保证结果的排序。

于 2019-03-12T06:50:59.987 回答