在hibernate中,我们往往会使用一对多等关联。
以一对多的Dept和Emp为例:
Dept.java:
package com.yun.hibernate.vo;
import java.util.HashSet;
import java.util.Set;
public class Dept {
private Integer deptId;
private String deptName;
private Set emps=new HashSet();
public Set getEmps() {
return emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
Emp.java:
package com.yun.hibernate.vo;
import java.util.Date;
public class Emp {
private Integer empId;
private String empName;
private Dept dept=new Dept();
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Dept.hbm.xml:
<?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="com.yun.hibernate.vo.Dept" table="dept"> <id name="deptId" column="deptid"> <generator class="native" /><!-- 自动增长的 --> </id> <property name="deptName" type="string"/> <set name="emps" inverse="true" cascade="all"> <key column="deptid" not-null="true"/> <one-to-many class="com.yun.hibernate.vo.Emp"/> </set> </class> </hibernate-mapping>
Emp.hbm.xml:
<?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="com.yun.hibernate.vo.Emp" table="emp"> <id name="EmpId" column="empid"> <generator class="native" /><!-- 自动增长的 --> </id> <property name="empName" type="string"/> <many-to-one name="dept" column="deptid" not-null="true"/> </class> </hibernate-mapping>
我们在Dept中为了实现一对多,所以设置了一个Set集合,但是我们知道,Set是无序的,而我们在遍历的时候,基本不可能需要无序的遍历,那么就需要对Set进行排序,我们可以使用实现类TreeSet,但是比较麻烦。
最好的解决办法是:直接在hbm文件中配置一下就可以进行排序了:
在Set上配置一个order-by=" *** ”属性就可以进行排序了,
<?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="com.yun.hibernate.vo.Dept" table="dept"> <id name="deptId" column="deptid"> <generator class="native" /><!-- 自动增长的 --> </id> <property name="deptName" type="string"/> <set name="emps" inverse="true" cascade="all" order-by="empid asc"> <key column="deptid" not-null="true"/> <one-to-many class="com.yun.hibernate.vo.Emp"/> </set> </class> </hibernate-mapping>
其中order-by="empid asc" empid是emp表中的字段,而不是Emp.hbm.xml中的属性。
当然排序的时候可以指定多个字段进行排序:
<set name="courses" inverse="true" order-by="weekday asc, period asc, roomId asc">
使用注解也可以进行排序,参考:
http://blog.csdn.net/jz19890704/article/details/7229333