Suhail Gupta
Suhail Gupta

Reputation: 23276

What does the 'sort' attribute do?

Following is the snippet from the hibernate mapping file, of the class named m1.

<class name="pojo.m1" table="m1">

  <id name="r_no">
      <generator class="increment" />
  </id>

  <property name="s_name" />

  <map name="map" table="m4" cascade="all" sort="pojo.m4">
      <key column="r_no" />
      <index-many-to-many class="pojo.m2" column="t1" /> <!-- r_no as index !-->
      <many-to-many class="pojo.m3" column="t2" /> <!-- r_no + total OR class m3 as key !-->
  </map>

</class>

In the map tag, there is an attribute named sort. What does it do ? Here it names the class that implements the comparator class.

public class m4 implements Comparator<m2> {

    @Override
    public int compare(m2 o1, m2 o2) {
        if(o1.getR_no() > o2.getR_no()) 
            return 1;
        else 
            return -1;
    }  
}

Upvotes: 0

Views: 73

Answers (1)

Andr&#233; Stannek
Andr&#233; Stannek

Reputation: 7863

From the official documentation:

sort (optional): specifies a sorted collection with natural sort order or a given comparator class.

It specifies that your mapped collection is sorted and by what criteria it is sorted. In your case a comparator is given that defines the order of two objects and therefore iterativly the order of all objects.

Upvotes: 2

Related Questions