Sort the Set set of Hibernate

Source: Internet
Author: User
Tags set set
In the following method: method 2 has been confirmed correct! You can specify the sorting method: <set name = "four" inverse = "true" lazy = "false" cascade = "all" Order-by = "id DESC"> sort the Set set of hibernate.

In hibernate, the Set set is used for one-to-many and multi-to-many associations, but sometimes objects in the Set set need to be sorted in the business. Of course, we can change the Set set to the list set to store objects, but if we use the list, we need to add a new field in the database. I don't want to change the data structure, so, how should we sort the set?

 

Department table (Dept) ----------- employee table (EMP)

Department pojo class:

Public classdept {

Private integer deptid;

Private string dname;

Private set EMPs = newhashset (0 );

}

 

Pojo class for employees:

Public classemp {

Private integer empid;

Private string ename;

Private dept;

}

 

Method 1:

Use treeset instead of hashset in pojo;

Private set EMPs = new treeset ();

It's easy, right, but why? Let's take a look at the differences between hashtable and treeset.

 

HashsetImplement the set interface, which is supported by a hash table (actually a hashmap instance. It does not guarantee the iteration sequence of the Set; in particular, it does not ensure that the order remains unchanged. This class allows the use of null elements. Hashset provides stable performance for basic operations, including add, remove, contains, and size. It is assumed that the hash function correctly distributes these elements in the bucket. The time required for performing iteration on this set is the "capacity" and proportion of the hashset instance size (number of elements) and the underlying hashmap instance (number of buckets. Therefore, if iteration performance is important, do not set the initial capacity too high (or set the loading factor too low ). The implementation of hashset is not synchronized. If multiple threads access a set at the same time and at least one thread modifies the setRequiredMaintain external synchronization.

 

TreesetClass to implement the set interface, which is supported by the treemap instance. This class ensures that the sorted set sorts the elements in ascending order, which may be sorted in the natural order of the elements according to different construction methods, or sort by the comparator provided when the set is created. Is an ordered set. elements are sorted in ascending order. By default, they are sorted in the natural order, which means that the elements in the treeset must implement the comparable interface. When we can construct a treeset object, pass the comparator object that implements the comparator Interface

 

 

Method 2:

Add order-by = "empid" to the <set> </set> element in Dept. HBM. xml"

 

<Set name = "EMPs" inverse = "true"Order-by = "empid">

<Key>

<Columnname = "deptid"/>

</Key>

<One-to-manyclass = "hwt. pojo. EMP"/>

</Set>

 

 

Method 3:

It is also modified in the configuration file:

Write a class first to implement the comparator interface and implement the compare method.

Public classHwtsorterImplementsComparator {

Public intCompare (objecto1, object O2 ){

EMP emp1 = (EMP) O1;

EMP emp2 = (EMP) O2;

// Sort by ID

Integer empid = emp1.getempid ();

Integer empid2 = emp2.getempid ();

ReturnEmpid> empid2? 1 :( empid = empid2? 0:-1 );

}

}

Similarly, add the <set> </set> element in Dept. HBM. XML to the dept configuration file.

<Set name = "EMPs" inverse = "true"Sort = "hwt. pojo. hwtsort">

<Key>

<Columnname = "deptid"/>

</Key>

<One-to-manyclass = "hwt. pojo. EMP"/>

</Set>

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.