hibernate多对一关系的配置(2)

 

Errata.java
package com.daacc.dao.errata;

import java.util.Date;

import com.daacc.dao.question.Question;
import com.daacc.dao.sort.Sort;

public class Errata implements java.io.Serializable {

    // Fields

    private Integer id;

 

    private Question question;

    private String page;

    private String description;

    private String source;

    private String correct;

    private Date date;

    private String state;

    private Sort sort;

    // Constructors

   
    public Errata() {
    }

    public Errata(Integer id) {
        this.id = id;
    }

   
    public Errata(Question question, String page, String description,
            String source, String correct, Date date, String state) {
        this.question = question;
        this.page = page;
        this.description = description;
        this.source = source;
        this.correct = correct;
        this.date = date;
        this.state = state;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

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

    public String getPage() {
        return this.page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getSource() {
        return this.source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getCorrect() {
        return this.correct;
    }

    public void setCorrect(String correct) {
        this.correct = correct;
    }

    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = super.hashCode();
        result = PRIME * result + ((correct == null) ? 0 : correct.hashCode());
        result = PRIME * result + ((date == null) ? 0 : date.hashCode());
        result = PRIME * result + ((description == null) ? 0 : description.hashCode());
        result = PRIME * result + ((id == null) ? 0 : id.hashCode());
        result = PRIME * result + ((page == null) ? 0 : page.hashCode());
        result = PRIME * result + ((question == null) ? 0 : question.hashCode());
        result = PRIME * result + ((source == null) ? 0 : source.hashCode());
        result = PRIME * result + ((state == null) ? 0 : state.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Errata other = (Errata) obj;
        if (correct == null) {
            if (other.correct != null)
                return false;
        } else if (!correct.equals(other.correct))
            return false;
        if (date == null) {
            if (other.date != null)
                return false;
        } else if (!date.equals(other.date))
            return false;
        if (description == null) {
            if (other.description != null)
                return false;
        } else if (!description.equals(other.description))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (page == null) {
            if (other.page != null)
                return false;
        } else if (!page.equals(other.page))
            return false;
        if (question == null) {
            if (other.question != null)
                return false;
        } else if (!question.equals(other.question))
            return false;
        if (source == null) {
            if (other.source != null)
                return false;
        } else if (!source.equals(other.source))
            return false;
        if (state == null) {
            if (other.state != null)
                return false;
        } else if (!state.equals(other.state))
            return false;
        return true;
    }

 

}
Errata.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.daacc.dao.errata.Errata" table="errata">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native"></generator>
        </id>
        <many-to-one name="question"
            class="com.daacc.dao.question.Question" fetch="select">
            <column name="question_id" not-null="true" unique="true" />
        </many-to-one>
        <many-to-one name="sort" cascade="save-update" outer-join="auto"
            class="com.daacc.dao.sort.Sort">
            <column name="sort_id" />
        </many-to-one>
        <property name="page" type="java.lang.String">
            <column name="page" length="11" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" length="300" />
        </property>
        <property name="source" type="java.lang.String">
            <column name="source" length="200" />
        </property>
        <property name="correct" type="java.lang.String">
            <column name="correct" length="300" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="date" length="10" />
        </property>
        <property name="state" type="java.lang.String">
            <column name="state" length="11" />
        </property>


    </class>
</hibernate-mapping>

GetErrtaBySortAction.java
package com.daacc.web.errata;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.daacc.dao.sort.Sort;
import com.daacc.facade.*;
import com.daacc.dao.errata.*;


public class GetErrtaBySortAction extends Action {
   

   
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        String sortId = request.getParameter("sortId");
        System.out.println("sortId:" + sortId);
        Sort sort = (Sort) BusFacade.sortBus.getSort(Integer.parseInt(sortId));
        Set set = sort.getErratas();
        Iterator it = set.iterator();
        List errList = new ArrayList();
        while (it.hasNext()) {
            errList.add(it.next());
        }
        request.setAttribute("errList", errList);
        for (int i = 0; i < errList.size(); i++) {
            Errata errata = (Errata) errList.get(i);
            System.out.println(errata.getId());
            System.out.println("errata.getDescription():"
                    + errata.getDescription());
        }
        return mapping.findForward("success");
    }
}
业务层代码


 

    public Sort getSort(int id) {

        Sort sort = null;

        try {
            sort = sortDao.get(new Integer(id));
        } catch (Exception e) {
            e.printStackTrace();

        }

        return sort;

    }
 SortDAOImp.java

    public Sort get(Integer id) {
        return (Sort) getHibernateTemplate().get(Sort.class, id);
    }

http://www.blogjava.net/sclsch/archive/2007/07/10/129414.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值