论坛首页 Java企业应用论坛

Hibernate Set集合排序

浏览 12392 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-12   最后修改:2008-12-12
本文探讨对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;
    }
}
}
   发表时间:2008-12-31  
一个排序搞这么多代码
0 请登录后投票
   发表时间:2009-01-01  
呵呵,说实在的不知所云。
Hibernate提供两种方式排序,一是orderby,这个是直接影响SQL,另外可以设置sort,它是对结果集进行排序。
0 请登录后投票
   发表时间:2009-01-01  
呵呵,回复比主贴有意思。
这是hibernate中的一种典型的实现set集合映射的排序
0 请登录后投票
   发表时间:2009-01-02  
其实可不可以配置完:

<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容器改为List容器?

因为set存入时讲速度,不保留顺序,但List保留顺序。
0 请登录后投票
   发表时间:2009-01-02  
没错,楼上的解决办法很明了
0 请登录后投票
   发表时间:2009-01-11  
也可用set 需要实现一个comparator 或者指定order by 字段
0 请登录后投票
   发表时间:2009-01-11  
不会吧,我这样配置都是可以有序的
0 请登录后投票
   发表时间:2009-01-12  
order by 和 sort配置项 都可以。
没有那么复杂
0 请登录后投票
   发表时间:2009-01-12  
PO实现Camparable接口,Set使用TreeSet就可以了……
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics