小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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.

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多