Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  语源科技BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks

Problem

We want a collection field belonging to an entity object to be sorted.

Solution

There are two possible solutions:

  • If you want the collection to be sorted when loaded, but not necessarily keep that sorting order all the time, use the OrderBy annotation where you set the actual ORDER BY SQL clause. In that case, the collection can be of any type (e.g. Collection).
  • If you want the collection to be sorted at all times, make it of type SortedSet or SortedMap and set the Sort annotation.

    • If the objects in the collection implements Comparable set type to SortType.NATURAL.
    • Otherwise set it to SortType.COMPARATOR and specify your own Comparator class in the comparator attribute.

Remarks

  • There exists an annotation with name OrderBy in both the javax.persistence package as well as the org.hibernate.annotations package. The former should be used.
posted on 2009-05-05 15:09 seal 阅读(244) 评论(0)  编辑  收藏 所属分类: Hibernate