sclsch

java备忘

BlogJava 首页 新随笔 联系 聚合 管理
  10 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks
种类Sort与勘误Errata是一对多的关系.
Sort.java
package com.daacc.dao.sort;

import java.util.HashSet;
import java.util.Set;

import com.daacc.dao.teacher.Teacher;

/**
 * Sort generated by MyEclipse Persistence Tools
 
*/

public class Sort implements java.io.Serializable {

    
// Fields

    
private Integer id;

    
private Integer parentId;

    
private String name;

    
private String description;

    
private Set erratas=new HashSet();

    
// Constructors

    
/** default constructor */
    
public Sort() {
    }

    
/** minimal constructor */
    
public Sort(Integer id) {
        
this.id = id;
    }

    
/** full constructor */
    
public Sort(Integer id, Integer parentid, String name, String description) {
        
this.id = id;
        
this.parentId = parentid;
        
this.name = name;
        
this.description = description;
        
// this.teachers = teachers;
    }

    
// Property accessors

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

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

    
public String getName() {
        
return this.name;
    }

    
public void setName(String name) {
        
this.name = name;
    }

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

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

    
public Integer getParentId() {
        
return parentId;
    }

    
public void setParentId(Integer parentId) {
        
this.parentId = parentId;
    }

    
public Set getErratas() {
        
return erratas;
    }

    
public void setErratas(Set erratas) {
        
this.erratas = erratas;
    }

    @Override
    
public int hashCode() {
        
final int PRIME = 31;
        
int result = super.hashCode();
        result 
= PRIME * result + ((description == null? 0 : description.hashCode());
        result 
= PRIME * result + ((id == null? 0 : id.hashCode());
        result 
= PRIME * result + ((name == null? 0 : name.hashCode());
        result 
= PRIME * result + ((parentId == null? 0 : parentId.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 Sort other = (Sort) obj;
        
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 (name == null) {
            
if (other.name != null)
                
return false;
        } 
else if (!name.equals(other.name))
            
return false;
        
if (parentId == null) {
            
if (other.parentId != null)
                
return false;
        } 
else if (!parentId.equals(other.parentId))
            
return false;
        
return true;
    }

}

Sort.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 - Hibernate Tools
-->

<hibernate-mapping>


    
<class name="com.daacc.dao.sort.Sort" table="sort">
        
<id name="id" type="integer">
            
<column name="id" />
            
<generator class="native" />
        
</id>

        
<property name="parentId" type="integer">
            
<column name="parent_id" not-null="true" />
        
</property>
        
<property name="name" type="string">
            
<column name="name" length="30" />
        
</property>
        
<property name="description" type="string">
            
<column name="description" length="300" />
        
</property>
        
<set name="erratas" inverse="false" lazy="true"
            cascade
="all-delete-orphan" outer-join="true">
            
<key>
                
<column name="sort_id" not-null="true" />
            
</key>
            
<one-to-many class="com.daacc.dao.errata.Errata" />
        
</set>

    
</class>

</hibernate-mapping>

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

import java.util.Date;

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

/**
 * Errata generated by MyEclipse Persistence Tools
 
*/

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

    
/** default constructor */
    
public Errata() {
    }

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

    
/** full constructor */
    
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
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/
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.*;

/**
 * MyEclipse Struts Creation date: 07-09-2007
 * 
 * XDoclet definition:
 * 
 * @struts.action validate="true"
 * @struts.action-forward name="success"
 *                        path="/WEB-INF/jsp/userarea/err/errata.jsp"
 
*/
public class GetErrtaBySortAction extends Action {
    
/*
     * Generated Methods
     
*/

    
/**
     * Method execute
     * 
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@return ActionForward
     
*/
    
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);
    }
posted on 2007-07-10 20:04 sclsch 阅读(3068) 评论(1)  编辑  收藏

Feedback

# re: hibernate多对一关系的配置及操作 2009-05-31 17:32 嘎嘎
也不用中文标识一下  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: