分享

6.1.6 Sorted and ordered collections (2) - ordered on database side

 moonboat 2009-02-12
Alternatively, instead of switching to the Sorted* interfaces (and the Tree*
implementations), you may want to work with a linked map and to sort elements
on the database side, not in memory. Keep the Map/HashMap declaration in the
Java class, and create the following mapping:
<map name="images" table="ITEM_IMAGE" order-by="IMAGENAME asc">
      <key column="ITEM_ID"/>
      <map-key column="IMAGENAME" type="string"/>
      <element type="string" column="FILENAME" not-null="true"/>
</map>
The expression in the order-by attribute is a fragment of an SQL order by
clause. In this case, Hibernate orders the collection elements by the IMAGENAME
column in ascending order during loading of the collection. You can even include
an SQL function call in the order-by attribute:
<map name="images" table="ITEM_IMAGE" order-by="lower(FILENAME) asc">
      <key column="ITEM_ID"/>
      <map-key column="IMAGENAME" type="string"/>
      <element type="string" column="FILENAME" not-null="true"/>
</map>
You can order by any column of the collection table. Internally, Hibernate uses a
LinkedHashMap, a variation of a map that preserves the insertion order of key elements.
In other words, the order that Hibernate uses to add the elements to the
collection, during loading of the collection, is the iteration order you see in your
application. The same can be done with a set: Hibernate internally uses a
LinkedHashSet. In your Java class, the property is a regular Set/HashSet, but
Hibernate’s internal wrapping with a LinkedHashSet is again enabled with the
order-by attribute:
<set name="images"   table="ITEM_IMAGE"   order-by="FILENAME asc">
      <key column="ITEM_ID"/>
      <element type="string" column="FILENAME" not-null="true"/>
</set>
You can also let Hibernate order the elements of a bag for you during collection
loading. Your Java collection property is either Collection/ArrayList or List/
ArrayList. Internally, Hibernate uses an ArrayList to implement a bag that preserves
insertion-iteration order:
<idbag name="images"   table="ITEM_IMAGE"   order-by="ITEM_IMAGE_ID desc">
      <collection-id type="long" column="ITEM_IMAGE_ID">
            <generator class="sequence"/>
      </collection-id>
      <key column="ITEM_ID"/>
      <element type="string" column="FILENAME" not-null="true"/>
</idbag>
The linked collections Hibernate uses internally for sets and maps are available
only in JDK 1.4 or later; older JDKs don’t come with a LinkedHashMap and
LinkedHashSet. Ordered bags are available in all JDK versions; internally, an
ArrayList is used.
In a real system, it’s likely that you’ll need to keep more than just the image
name and filename. You’ll probably need to create an Image class for this extra
information. This is the perfect use case for a collection of components.
 
Map/HashMap ->LinkedHashMap,
Set/HashSet      ->LinkedHashSet.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多