hibernate中一对多Set的排序问题

hibernate中,我们往往会使用一对多等关联。

 

 

以一对多的DeptEmp为例:

 

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" empidemp表中的字段,而不是Emp.hbm.xml中的属性。

 

 

当然排序的时候可以指定多个字段进行排序:

<set name="courses" inverse="true" order-by="weekday asc, period asc, roomId asc">

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值