hibernate中,一对多的表中有set类型字段,如何处理排序问题

第一张表:投票主题(VoteTheme)

   实体:

   

package com.topsoft.icf.domain.entities;
import java.util.Date;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: 
 * Date: 2010-4-28
 * Time: 16:03:48
 * To change this template use File | Settings | File Templates.
 * @hibernate.mapping default-lazy="false"
 * @hibernate.meta attribute="class-description" value="投票主题"
 * @hibernate.class table="JL_WLTPZT"
 */
public class VoteTheme {
    private Long id;//标识
    private String theme;//主题
    private Date begintime;//开始时间
    private Date endtime;//截止时间
    private int state=0;//0:起草状态
    private int type;//类型
    private Date releasetime;//发布时间

    private Long userid;//创建人标识
    private String username;//创建人名称
    private Long orgid;//创建机构标识
    private String orgname;//创建的机构名称
    private Long parentorgid;//创建上级机构
    private Long morgid;//管理机关
    private Date createtime;//创建时间

    private String remark;//备注说明

    private Set voteItem;//投票项

    public final static int QC=0,//起草
                               FB=1,//发布
                               GB=2,//关闭
                               SC=3;//作废
    public final static int DAXBJM=1,//单选不记名
                               DAXJM=2,//单选记名
                               DOXBJM=3,//多选不记名
                               DOXJM=4;//多选记名
    /**
     * @hibernate.id generator-class="sequence" column="BS"
     * @hibernate.meta attribute="field-description" value="标识"
     * @hibernate.generator-param name="sequence" value="SEQ_JL"
     */
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    /**
     * @hibernate.property column="ZTMC" type="string" length="300" not-null="true"
     * @hibernate.meta attribute="field-description" value="主题名称"
     */
    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }
    /**
     * @hibernate.property column="KSSJ" type="timestamp" not-null="false"
     * @hibernate.meta attribute="field-description" value="开始时间"
     */
    public Date getBegintime() {
        if(begintime==null) return null;
        else return (Date)begintime.clone();
    }

    public void setBegintime(Date begintime) {
        if(begintime==null) this.begintime=null;
        else this.begintime = (Date)begintime.clone();
    }
    /**
     * @hibernate.property column="JZSJ" type="timestamp" not-null="false"
     * @hibernate.meta attribute="field-description" value="截止时间"
     */
    public Date getEndtime() {
        if(endtime==null)return null;
        else return (Date)endtime.clone();
    }

    public void setEndtime(Date endtime) {
        if(endtime==null) this.endtime=null;
        else this.endtime = (Date)endtime.clone();
    }
    /**
     * @hibernate.property column="ZT" type="int" not-null="true"
     * @hibernate.meta attribute="field-description" value="状态"
     */
    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
    /**
     * @hibernate.property column="LX" type="int" not-null="true"
     * @hibernate.meta attribute="field-description" value="类型"
     */
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    /**
     * @hibernate.property column="FBSJ" type="timestamp" not-null="false"
     * @hibernate.meta attribute="field-description" value="发布时间"
     */
    public Date getReleasetime() {
        if(releasetime==null)return null;
        else return (Date)releasetime.clone();
    }

    public void setReleasetime(Date releasetime) {
        if(releasetime==null) this.releasetime=null;
        else this.releasetime = (Date)releasetime.clone();        
    }

    /**
     * @hibernate.property column="CJRBS" type="long" not-null="true"
     * @hibernate.meta attribute="field-description" value="创建人标识"
     */
    public Long getUserid() {
        return userid;
    }

    public void setUserid(Long userid) {
        this.userid = userid;
    }

    /**
     * @hibernate.property column="CJRMC" type="string" length="100" not-null="false"
     * @hibernate.meta attribute="field-description" value="创建人名称"
     */
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @hibernate.property column="CJJGBS" type="long" not-null="false"
     * @hibernate.meta attribute="field-description" value="创建机构标识"
     */
    public Long getOrgid() {
        return orgid;
    }

    public void setOrgid(Long orgid) {
        this.orgid = orgid;
    }

    /**
     * @hibernate.property column="CJJGMC" type="string" length="200" not-null="false"
     * @hibernate.meta attribute="field-description" value="创建机构名称"
     */
    public String getOrgname() {
        return orgname;
    }

    public void setOrgname(String orgname) {
        this.orgname = orgname;
    }

    /**
     * @hibernate.property column="CJSJJGBS" type="long" not-null="false"
     * @hibernate.meta attribute="field-description" value="创建上级机构标识"
     */
    public Long getParentorgid() {
        return parentorgid;
    }

    public void setParentorgid(Long parentorgid) {
        this.parentorgid = parentorgid;
    }

    /**
     * @hibernate.property column="CJDWBS" type="long" not-null="false"
     * @hibernate.meta attribute="field-description" value="创建单位标识"
     */
    public Long getMorgid() {
        return morgid;
    }

    public void setMorgid(Long morgid) {
        this.morgid = morgid;
    }

    /**
     * @hibernate.property column="CJSJ" type="timestamp" not-null="false"
     * @hibernate.meta attribute="field-description" value="创建时间"
     */
     public Date getCreatetime() {
        if(createtime==null) return null;
       return (Date) createtime.clone();
    }

    public void setCreatetime(Date createtime) {
        if(createtime==null) this.createtime = null;
        else this.createtime = (Date)createtime.clone();
    }
    /**
     * @hibernate.property column="BZSM" type="string" length="600" not-null="false"
     * @hibernate.meta attribute="field-description" value="备注说明"
     */
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
    /**
     * @hibernate.collection-one-to-many class="com.topsoft.icf.domain.entities.VoteItem"
     * @hibernate.collection-key column="WLTPZT_BS"
     * @hibernate.set name="voteItem" table="JL_WLTPX"
     * lazy="false" cascade="all-delete-orphan" order-by="bs asc" sort="unsorted"
     */
    public Set getVoteItem() {
        return voteItem;
    }

    public void setVoteItem(Set voteItem) {
        this.voteItem = voteItem;
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        VoteTheme voteTheme = (VoteTheme) o;

        if (state != voteTheme.state) return false;
        if (type != voteTheme.type) return false;
        if (begintime != null ? !begintime.equals(voteTheme.begintime) : voteTheme.begintime != null) return false;
        if (createtime != null ? !createtime.equals(voteTheme.createtime) : voteTheme.createtime != null) return false;
        if (endtime != null ? !endtime.equals(voteTheme.endtime) : voteTheme.endtime != null) return false;
        if (id != null ? !id.equals(voteTheme.id) : voteTheme.id != null) return false;
        if (morgid != null ? !morgid.equals(voteTheme.morgid) : voteTheme.morgid != null) return false;
        if (orgid != null ? !orgid.equals(voteTheme.orgid) : voteTheme.orgid != null) return false;
        if (orgname != null ? !orgname.equals(voteTheme.orgname) : voteTheme.orgname != null) return false;
        if (parentorgid != null ? !parentorgid.equals(voteTheme.parentorgid) : voteTheme.parentorgid != null)
            return false;
        if (releasetime != null ? !releasetime.equals(voteTheme.releasetime) : voteTheme.releasetime != null)
            return false;
        if (remark != null ? !remark.equals(voteTheme.remark) : voteTheme.remark != null) return false;
        if (theme != null ? !theme.equals(voteTheme.theme) : voteTheme.theme != null) return false;
        if (userid != null ? !userid.equals(voteTheme.userid) : voteTheme.userid != null) return false;
        if (username != null ? !username.equals(voteTheme.username) : voteTheme.username != null) return false;

        return true;
    }

    
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (theme != null ? theme.hashCode() : 0);
        result = 31 * result + (begintime != null ? begintime.hashCode() : 0);
        result = 31 * result + (endtime != null ? endtime.hashCode() : 0);
        result = 31 * result + state;
        result = 31 * result + type;
        result = 31 * result + (releasetime != null ? releasetime.hashCode() : 0);
        result = 31 * result + (userid != null ? userid.hashCode() : 0);
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (orgid != null ? orgid.hashCode() : 0);
        result = 31 * result + (orgname != null ? orgname.hashCode() : 0);
        result = 31 * result + (parentorgid != null ? parentorgid.hashCode() : 0);
        result = 31 * result + (morgid != null ? morgid.hashCode() : 0);
        result = 31 * result + (createtime != null ? createtime.hashCode() : 0);
        result = 31 * result + (remark != null ? remark.hashCode() : 0);
        return result;
    }
}

 hibernate映射文件:

  

<?xml version="1.0" encoding="gb2312"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
        default-lazy="false"
>
    <class
        name="com.topsoft.icf.domain.entities.VoteTheme"
        table="JL_WLTPZT"
    >
        <meta attribute="class-description">投票主题</meta>

        <id
            name="id"
            column="BS"
            type="java.lang.Long"
        >
            <meta attribute="field-description">标识</meta>
            <generator class="sequence">
                <param name="sequence">SEQ_JL</param>
              <!--  
                  To add non XDoclet generator parameters, create a file named 
                  hibernate-generator-params-VoteTheme.xml 
                  containing the additional parameters and place it in your merge dir. 
              --> 
            </generator>
        </id>

        <property
            name="theme"
            type="string"
            update="true"
            insert="true"
            column="ZTMC"
            length="300"
            not-null="true"
        >
            <meta attribute="field-description">主题名称</meta>
        </property>

        <property
            name="begintime"
            type="timestamp"
            update="true"
            insert="true"
            column="KSSJ"
            not-null="false"
        >
            <meta attribute="field-description">开始时间</meta>
        </property>

        <property
            name="endtime"
            type="timestamp"
            update="true"
            insert="true"
            column="JZSJ"
            not-null="false"
        >
            <meta attribute="field-description">截止时间</meta>
        </property>

        <property
            name="state"
            type="int"
            update="true"
            insert="true"
            column="ZT"
            not-null="true"
        >
            <meta attribute="field-description">状态</meta>
        </property>

        <property
            name="type"
            type="int"
            update="true"
            insert="true"
            column="LX"
            not-null="true"
        >
            <meta attribute="field-description">类型</meta>
        </property>

        <property
            name="releasetime"
            type="timestamp"
            update="true"
            insert="true"
            column="FBSJ"
            not-null="false"
        >
            <meta attribute="field-description">发布时间</meta>
        </property>

        <property
            name="userid"
            type="long"
            update="true"
            insert="true"
            column="CJRBS"
            not-null="true"
        >
            <meta attribute="field-description">创建人标识</meta>
        </property>

        <property
            name="username"
            type="string"
            update="true"
            insert="true"
            column="CJRMC"
            length="100"
            not-null="false"
        >
            <meta attribute="field-description">创建人名称</meta>
        </property>

        <property
            name="orgid"
            type="long"
            update="true"
            insert="true"
            column="CJJGBS"
            not-null="false"
        >
            <meta attribute="field-description">创建机构标识</meta>
        </property>

        <property
            name="orgname"
            type="string"
            update="true"
            insert="true"
            column="CJJGMC"
            length="200"
            not-null="false"
        >
            <meta attribute="field-description">创建机构名称</meta>
        </property>

        <property
            name="parentorgid"
            type="long"
            update="true"
            insert="true"
            column="CJSJJGBS"
            not-null="false"
        >
            <meta attribute="field-description">创建上级机构标识</meta>
        </property>

        <property
            name="morgid"
            type="long"
            update="true"
            insert="true"
            column="CJDWBS"
            not-null="false"
        >
            <meta attribute="field-description">创建单位标识</meta>
        </property>

        <property
            name="createtime"
            type="timestamp"
            update="true"
            insert="true"
            column="CJSJ"
            not-null="false"
        >
            <meta attribute="field-description">创建时间</meta>
        </property>

        <property
            name="remark"
            type="string"
            update="true"
            insert="true"
            column="BZSM"
            length="600"
            not-null="false"
        >
            <meta attribute="field-description">备注说明</meta>
        </property>

        <set
            name="voteItem"
            table="JL_WLTPX"
            lazy="false"
            cascade="all-delete-orphan"
            order-by="bs asc"
            sort="unsorted"
        >

            <key
                column="WLTPZT_BS"
            >
            </key>

            <one-to-many
                  class="com.topsoft.icf.domain.entities.VoteItem"
            />

        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-VoteTheme.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

第二张表:投票项(VoteItem)

     实体:

package com.topsoft.icf.domain.entities;

/**
 * Created by IntelliJ IDEA.
 * User: shiduanjun
 * Date: 2010-4-28
 * Time: 16:04:08
 * To change this template use File | Settings | File Templates.
 *
 * @hibernate.mapping default-lazy="false"
 * @hibernate.meta attribute="class-description" value="投票项"
 * @hibernate.class table="JL_WLTPX"
 */
public class VoteItem implements Comparable {
    private Long id;//标识
    private String itemname;//项名
    private Long num;//得票数
    private VoteTheme voteTheme;//投票主题

    /**
     * @hibernate.id generator-class="sequence" column="BS"
     * @hibernate.meta attribute="field-description" value="标识"
     * @hibernate.generator-param name="sequence" value="SEQ_JL"
     */
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @hibernate.property column="XM" type="string" length="150" not-null="true"
     * @hibernate.meta attribute="field-description" value="项名"
     */
    public String getItemname() {
        return itemname;
    }

    public void setItemname(String itemname) {
        this.itemname = itemname;
    }

    /**
     * @hibernate.property column="DPS" type="long" not-null="false"
     * @hibernate.meta attribute="field-description" value="得票数"
     */
    public Long getNum() {
        return num;
    }

    public void setNum(Long num) {
        this.num = num;
    }

    /**
     * @hibernate.many-to-one class="com.topsoft.icf.domain.entities.VoteTheme"
     * @hibernate.meta attribute="field-description" value="投票主题"
     * @hibernate.column name="WLTPZT_BS"
     */
    public VoteTheme getVoteTheme() {
        return voteTheme;
    }

    public void setVoteTheme(VoteTheme voteTheme) {
        this.voteTheme = voteTheme;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        VoteItem voteItem = (VoteItem) o;

        if (id != null ? !id.equals(voteItem.id) : voteItem.id != null) return false;
        if (itemname != null ? !itemname.equals(voteItem.itemname) : voteItem.itemname != null) return false;
        if (num != null ? !num.equals(voteItem.num) : voteItem.num != null) return false;
        if (voteTheme != null ? !voteTheme.equals(voteItem.voteTheme) : voteItem.voteTheme != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (itemname != null ? itemname.hashCode() : 0);
        result = 31 * result + (num != null ? num.hashCode() : 0);
        result = 31 * result + (voteTheme != null ? voteTheme.hashCode() : 0);
        return result;
    }

    public int compareTo(Object o) {
        VoteItem vi = (VoteItem) o;
        return this.itemname.compareTo(vi.getItemname());
    }
}

 hibernate映射文件:

     

<?xml version="1.0" encoding="gb2312"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
        default-lazy="false"
>
    <class
        name="com.topsoft.icf.domain.entities.VoteItem"
        table="JL_WLTPX"
    >
        <meta attribute="class-description">投票项</meta>

        <id
            name="id"
            column="BS"
            type="java.lang.Long"
        >
            <meta attribute="field-description">标识</meta>
            <generator class="sequence">
                <param name="sequence">SEQ_JL</param>
              <!--  
                  To add non XDoclet generator parameters, create a file named 
                  hibernate-generator-params-VoteItem.xml 
                  containing the additional parameters and place it in your merge dir. 
              --> 
            </generator>
        </id>

        <property
            name="itemname"
            type="string"
            update="true"
            insert="true"
            column="XM"
            length="150"
            not-null="true"
        >
            <meta attribute="field-description">项名</meta>
        </property>

        <property
            name="num"
            type="long"
            update="true"
            insert="true"
            column="DPS"
            not-null="false"
        >
            <meta attribute="field-description">得票数</meta>
        </property>

        <many-to-one
            name="voteTheme"
            class="com.topsoft.icf.domain.entities.VoteTheme"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
        >
            <meta attribute="field-description">投票主题</meta>
            <column
                name="WLTPZT_BS" 
            />
        </many-to-one>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-VoteItem.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

 首先这两张表关系:VoteTheme对VoteItem:一对多的关系;VoteItem对VoteTheme:多对一关系 。

大家要注意的地方:

一:

   在VoteItem实体中应该导入接口:Comparable;

二:

   在VoteItem实体类中要重写Comparable接口中的 public int compareTo(Object o)方法;

 

  public int compareTo(Object o) {
        VoteItem vi = (VoteItem) o;
        return this.itemname.compareTo(vi.getItemname());
    }

 

我这是根据投票项名称进行排序,当你进行保存和查看信息的时候,后台已经进行排序。这样你可以根据直接的情况来处理set排序问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值