Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(\)

时间:2021-07-27 22:52:26

1.

Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)

2.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" >
<column name="NAME" length="15" />
</property> <property name="age" type="int" >
<column name="AGE" />
</property> <set name="images" table="IMAGES" lazy="true" sort="natural" > <!--
<set name="images" table="IMAGES" lazy="true" sort="mypack.ReverseStringComparator">
--> <key column="MONKEY_ID" />
<element column="FILENAME" type="string" not-null="true"/>
</set> </class> </hibernate-mapping>

3.

 package mypack;

 import java.io.Serializable;
import java.util.Set;
import java.util.TreeSet; public class Monkey implements Serializable {
private Long id;
private String name;
private int age;
private Set images=new TreeSet(); /** full constructor */
public Monkey(String name, int age,Set images) {
this.name = name;
this.age=age;
this.images = images;
} /** default constructor */
public Monkey() {
} /** minimal constructor */
public Monkey(Set images) {
this.images = images;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return this.age;
} public void setAge(int age) {
this.age = age;
} public Set getImages() {
return this.images;
} public void setImages(Set images) {
this.images = images;
} }

4.

 package mypack;

 import java.util.*;
public class ReverseStringComparator implements Comparator{ public int compare(Object o1,Object o2){
String s1=(String)o1;
String s2=(String)o2; if(s1.compareTo(s2)>0) return -1;
if(s1.compareTo(s2)<0) return 1; return 0;
}
}

5.