2

我正在使用休眠。

我有一对多的关系。

@OneToMany(mappedBy = "enrollmentSetupCategory", fetch = FetchType.EAGER, cascade = CascadeType.ALL )
    private Set<EnrollmentSetupCategoryLevel> enrollmentSetupCategoryLevels;

我的问题是我在这里内部使用了哪个 set 实现?如果我扩展可比较的接口,返回的对象是否会被排序?还是我必须自己排序?

4

2 回答 2

5

Hibernate Supports sorted collection mapping, you can specify your comparator to do the sorting for you.

From the docs

Hibernate supports collections implementing java.util.SortedMap and java.util.SortedSet. You must specify a comparator in the mapping file:

<set name="aliases" 
            table="person_aliases" 
            sort="natural">
    <key column="person"/>
    <element column="name" type="string"/>
</set>

<map name="holidays" sort="my.custom.HolidayComparator">
    <key column="year_id"/>
    <map-key column="hol_name" type="string"/>
    <element column="hol_date" type="date"/>
</map>

Allowed values of the sort attribute are unsorted, natural and the name of a class implementing java.util.Comparator.

Sorted collections actually behave like java.util.TreeSet or java.util.TreeMap.

Edit Annotation From Docs, check

3.4.6. Collection related annotations

 @Sort(type = SortType.COMPARATOR, comparator = TicketComparator.class)
于 2012-04-27T06:10:25.453 回答
2

您将使用 Hibernate 自己的实现Set- org.hibernate.collection.PersistentSet http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/collection/PersistentSet.html 这个类没有实现Comparable接口,所以你应该对其进行排序由您自己或将其转换为 set 的一些可排序实现(例如TreeSet)。

于 2012-04-27T06:24:17.360 回答