Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1.
2.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Monkey" table="MONKEYS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="name" type="string" > 13 <column name="NAME" length="15" /> 14 </property> 15 16 <property name="age" type="int" > 17 <column name="AGE" /> 18 </property> 19 20 21 <set name="images" table="IMAGES" lazy="true" sort="natural" > 22 23 <!-- 24 <set name="images" table="IMAGES" lazy="true" sort="mypack.ReverseStringComparator"> 25 --> 26 27 <key column="MONKEY_ID" /> 28 <element column="FILENAME" type="string" not-null="true"/> 29 </set> 30 31 </class> 32 33 </hibernate-mapping>
3.
1 package mypack; 2 3 import java.io.Serializable; 4 import java.util.Set; 5 import java.util.TreeSet; 6 7 public class Monkey implements Serializable { 8 private Long id; 9 private String name; 10 private int age; 11 private Set images=new TreeSet(); 12 13 /** full constructor */ 14 public Monkey(String name, int age,Set images) { 15 this.name = name; 16 this.age=age; 17 this.images = images; 18 } 19 20 /** default constructor */ 21 public Monkey() { 22 } 23 24 /** minimal constructor */ 25 public Monkey(Set images) { 26 this.images = images; 27 } 28 29 public Long getId() { 30 return this.id; 31 } 32 33 public void setId(Long id) { 34 this.id = id; 35 } 36 37 public String getName() { 38 return this.name; 39 } 40 41 public void setName(String name) { 42 this.name = name; 43 } 44 45 public int getAge() { 46 return this.age; 47 } 48 49 public void setAge(int age) { 50 this.age = age; 51 } 52 53 public Set getImages() { 54 return this.images; 55 } 56 57 public void setImages(Set images) { 58 this.images = images; 59 } 60 61 }
4.
1 package mypack; 2 3 import java.util.*; 4 public class ReverseStringComparator implements Comparator{ 5 6 public int compare(Object o1,Object o2){ 7 String s1=(String)o1; 8 String s2=(String)o2; 9 10 if(s1.compareTo(s2)>0) return -1; 11 if(s1.compareTo(s2)<0) return 1; 12 13 return 0; 14 } 15 }
5.
You can do anything you set your mind to, man!