Hibernate Set集合排序

本文探讨对Hibernate的set集合进行排序。假如有两张表:课程信息表(ECH_LEARN_COURSE)和课程分类信息表(ECH_LEARN_COUCATEGORY),它们之间是一对多的关系。对应的java类分别为EchLearnCourse和EchLearnCoucategory。EchLearnCoucategory有一个Set属性 echLearnCourse,需要对echLearnCourse按课程的排序位置(ORDER_NUM)进行排序。首先使用数据库排序:
<set name="echLearnCourses" order-by="ORDER_NUM asc" inverse="true" lazy="true">
       <cache usage="read-write"/>
       <key>
           <column name="CATEGORYID" length="32" not-null="true"/>
       </key> 
       <one-to-many class="cn.echineseblcu.learning.persistence.EchLearnCourse"  />
</set>

但输出的集合无序,无奈,用内存排序吧
<set name="echLearnCourses" inverse="true" lazy="true" sort="cn.echineseblcu.learning.persistence.CourseComparator">
       <cache usage="read-write" />
       <key>
           <column name="CATEGORYID" length="32" not-null="true"/>
       </key> 
       <one-to-many class="cn.echineseblcu.learning.persistence.EchLearnCourse"  />
</set>
输出的集合依然无序。没辙了,那就用TreeSet试试:
即在映射类中定义相应的TreeSet属性echLearnCoursesTreeSet,让EchLearnCoucategory类实现Comparable接口。在EchLearnCoucategory中定义内部类CourseComparator,将EchLearnCourse 按orderNum排序。
     public TreeSet<EchLearnCourse> getEchLearnCoursesTreeSet() {
echLearnCoursesTreeSet.addAll(echLearnCourses);
return echLearnCoursesTreeSet;
    }
     public class CourseComparator implements Comparator{
    public int compare(Object o1,Object o2){
      EchLearnCourse c1=(EchLearnCourse)o1;
      EchLearnCourse c2=(EchLearnCourse)o2;
        if(c1.getOrderNum().compareTo(c2.getOrderNum())>0)
        return 1;
        if(c1.getOrderNum().compareTo(c2.getOrderNum())<0)
        return -1;
        return 0;
    }
}
输出某分类下的课程时使用echLearnCoursesTreeSet,而不用echLearnCourses。

具体映射文件及java类如下:
课程信息表映射文件及对应的java类:
<!--EchLearnCourse.hbm.xml-->
<hibernate-mapping>
    <class name="cn.echineseblcu.learning.persistence.EchLearnCourse" table="ECH_LEARN_COURSE" schema="ECHINESE2">
        <cache usage="read-write"/>
        <id name="id" type="java.lang.String">
            <column name="ID" length="32" />
            <generator class="uuid.hex" />
        </id>
        <many-to-one name="echLearnCoucategory" class="cn.echineseblcu.learning.persistence.EchLearnCoucategory" fetch="select">
            <column name="CATEGORYID" length="32" />
        </many-to-one>
        。。。。。。         
    </class>
</hibernate-mapping>

<!--EchLearnCourse.java-->
public class EchLearnCourse extends CommonBean implements java.io.Serializable,Comparable {
    // Fields   

     public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}

     private EchLearnCoucategory echLearnCoucategory;
     。。。。。。    
     // Constructors

    /** default constructor */
    public EchLearnCourse() {
    }
   
    public EchLearnCourse(String id) {
    this.id = id;
    }

    。。。。。。
    public EchLearnCoucategory getEchLearnCoucategory() {
        return this.echLearnCoucategory;
    }
   
    public void setEchLearnCoucategory(EchLearnCoucategory echLearnCoucategory) {
        this.echLearnCoucategory = echLearnCoucategory;
    }

    。。。。。。
    public boolean equals(Object o)
    {
        return EqualsBuilder.reflectionEquals(this, o);
    }
   
    public int hashCode()
    {
        return HashCodeBuilder.reflectionHashCode(this);
    }

}
课程分类信息表映射文件及对应的java类:
<!--EchLearnCoucategory.hbm.xml-->
<hibernate-mapping>
    <class name="cn.echineseblcu.learning.persistence.EchLearnCoucategory" table="ECH_LEARN_COUCATEGORY" schema="ECHINESE2">
        <cache usage="read-write"/>
        <id name="id" type="java.lang.String">
            <column name="ID" length="32" />
            <generator class="uuid.hex" />
        </id>
        。。。。。。    //省略若干属性  
        <set name="echLearnCourses" where="delflag='02'" inverse="true" lazy="true">
            <cache usage="read-write"/>
            <key>
                <column name="CATEGORYID" length="32" not-null="true"/>
            </key>
         
            <one-to-many class="cn.echineseblcu.learning.persistence.EchLearnCourse"  />
        </set>

    </class>
</hibernate-mapping>

<!--EchLearnCoucategory.java-->

public class EchLearnCoucategory extends CommonBean implements java.io.Serializable,Comparable {

// Fields

public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}

private Set echLearnCourses = new HashSet(0);

private TreeSet<EchLearnCourse> echLearnCoursesTreeSet = new TreeSet<EchLearnCourse>(new CourseComparator());

private Set echLearnCoucategoryI18ns = new HashSet(0);

// Constructors

/** default constructor */
public EchLearnCoucategory() {
}
        
。。。。。。//省略若干属性的getter和setter方法

public Set getEchLearnCourses() {
return this.echLearnCourses;
}

public void setEchLearnCourses(Set echLearnCourses) {
this.echLearnCourses = echLearnCourses;
}

public TreeSet<EchLearnCourse> getEchLearnCoursesTreeSet() {
echLearnCoursesTreeSet.addAll(echLearnCourses);
return echLearnCoursesTreeSet;
}

public void setEchLearnCoursesTreeSet(
   TreeSet<EchLearnCourse> echLearnCoursesTreeSet) {
   this.echLearnCoursesTreeSet = echLearnCoursesTreeSet;
}

public class CourseComparator implements Comparator{
    public int compare(Object o1,Object o2){
      EchLearnCourse c1=(EchLearnCourse)o1;
      EchLearnCourse c2=(EchLearnCourse)o2;
        if(c1.getOrderNum().compareTo(c2.getOrderNum())>0)
        return 1;
        if(c1.getOrderNum().compareTo(c2.getOrderNum())<0)
        return -1;
        return 0;
    }
}
}

你可能感兴趣的:(Hibernate,xml,sun)