JPA / hibernate排序集合@OrderBy vs @Sort [英] JPA/hibernate sorted collection @OrderBy vs @Sort

查看:1087
本文介绍了JPA / hibernate排序集合@OrderBy vs @Sort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个子对象的集合(这里是猫小猫的例子)。

I would like to have a collection of child objects (here cat-kitten example) that are ordered. And keep their order on adding of new elements.

@Entity 
public class Cat {
  @OneToMany(mappedBy = "cat", cascade = CascadeType.ALL)
  @OrderBy("name ASC")
  private List<Kitten> kittens;

  public void setKittens(List<Kitten> kittens) { this.kittens = kittens; }
  public List<Kitten> getKittens() { return kittens; } 
}

当我 cat.getKittens.add newKitten)按名称排序将被破坏。

When I do cat.getKittens.add(newKitten) the order by name will be broken.

有可能让hibernate做保持集合总是有序的工作吗?通过使用 @Sort hibernate 注释?

@Sort有一个缺点,它迫使你实现Comparable接口...
什么是正确的纯JPA的方式来做呢?将所有内容保存到数据库并重新加载?
合并@OrderBy和@Sort有意义吗?

Is it possible to let hibernate do the work of keeping the collection always ordered? By using the @Sort hibernate annotation?
@Sort has the disadvantage that it forces you to implement Comparable interface ... What would be the right 'pure JPA' way to do that? Saving everything to DB and reloading it? Makes it sense to combine @OrderBy and @Sort?

更新
现在的解决方案是将@ OrderBy和@Sort。 @OrderBy导致一个 ORDER BY 子句在生成的SQL更好的性能(我认为java是排序插入到已排序的容器再次,但应该因为元素已经排序)
@与一个实现的 Comparable 接口一起导致一个总是排序的容器。注意,我现在使用 SortedSet 而不是列表。这里更新的代码:

Update Solution up to now is to combine @OrderBy and @Sort. @OrderBy leads to a ORDER BY clause in the generated SQL which is better for performance (I assume that java is "sorting" again on inserting into the sorted container, but that should be much faster because the elements are already sorted) @Sort together with an implemented Comparable interface leads to a always sorted container. Note that I use now SortedSet instead of List. Here the updated Code:

@Entity 
public class Cat {
  @OneToMany(mappedBy = "cat", cascade = CascadeType.ALL)
  @OrderBy("name ASC")
  @Sort(type = SortType.NATURAL)
  private SortedSet<Kitten> kittens;

  public void setKittens(SortedSet<Kitten> kittens) { this.kittens = kittens; }
  public SortedSet<Kitten> getKittens() { return kittens; } 
}


推荐答案

非标准注释,可以使 kittens 使用一些排序的 Collection 实现。这将确保 kittens 始终按排序顺序。像这样:

If you want to avoid non-standard annotations, you could make kittens use some sorted Collection implementation. This would ensure that kittens is always in sorted order. Something like this:

@Entity 
public class Cat {
  @OneToMany(mappedBy = "cat", cascade = CascadeType.ALL)
  @OrderBy("name ASC")
  private SortedSet<Kitten> kittens = new TreeSet<>();
}

注意,这种方法还需要 Kitten 以实现 Comparable (或者,您可以将 Comparator 传递到您的 TreeSet 构造函数)。此外,我使用设置,因为我不知道任何标准排序列表实现,假设 Cat 在其枯枝落叶= p中没有任何克隆。

Note that this approach also requires Kitten to implement Comparable (alternatively, you could pass a Comparator to your TreeSet constructor). Also, I'm using a Set because I'm not aware of any standard sorted List implementation and I'm assuming the Cat does not have any clones in its litter =p.

更新:
我不知道如何挑剔的Hibernate是与其getter / setter定义,但使用EclipseLink我已经能够删除一个setter完全和包装列表由我的getter在 Collections.unmodifiableList(...)调用中返回。然后我定义了修改集合的特殊方法。你可以做同样的事情,强制调用者使用一个按照排序顺序插入元素的add方法。如果Hibernate抱怨没有getter / setter,也许你可以更改访问修饰符?我想这取决于你愿意去避免非标准依赖程度。

Update: I'm not sure how picky Hibernate is with its getter/setter definitions, but with EclipseLink I've been able to remove a setter entirely and wrap the List returned by my getter in a Collections.unmodifiableList(...) call. I then defined special methods for modifying the collection. You could do the same thing and force callers to use an add method that inserts elements in sorted order. If Hibernate complains about not having the getter/setter, maybe you could change the access modifier? I guess it comes down to how far you're willing to go to avoid non standard dependencies.

这篇关于JPA / hibernate排序集合@OrderBy vs @Sort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆