【问题标题】:Spring JPA : @OrderBy doesn't work when using saveAndFlushSpring JPA:使用 saveAndFlush 时 @OrderBy 不起作用
【发布时间】:2019-03-12 06:41:48
【问题描述】:

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

保存此数据时
persistedObject = saveAndFlush(MyCustomObject);

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

代码sn-p:

@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); 

【问题讨论】:

    标签: java spring hibernate spring-data-jpa hibernate-mapping


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      相关资源
      最近更新 更多