打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
6.1.6 Sorted and ordered collections (2) - ordered on database side
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.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Hibernate入门之Set与Map的排序
Hibernate中的column别和数据库中的关键字一样
EHCache 单独使用
简单的在Java中使用SQLite的教程
SQL Server 数据库基础编程
CCTV(大裤衩)结构设计全程记录(pic+text)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服