`
llflove1988
  • 浏览: 42937 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Hibernate多表级联更新

阅读更多
1.表映射关系
主表:NameREQ
<hibernate-mapping>
    <class name="com.cw.hibernate.entity.NameREQ" table="NAMEREQ" schema="LLF">
        <comment>名称信息表</comment>
        <id name="pripid" type="java.lang.Long">
            <column name="PRIPID"/>
            <generator class="native"></generator>
        </id>
        <property name="entname" type="java.lang.String">
            <column name="ENTNAME" length="100" not-null="true">
                <comment>企业(机构)名称</comment>
            </column>
        </property>
        <property name="namedistrict" type="java.lang.String">
            <column name="NAMEDISTRICT" length="40" not-null="true">
                <comment>名称区划</comment>
            </column>
        </property>
        <property name="enttra" type="java.lang.String">
            <column name="ENTTRA" length="40" not-null="true">
                <comment>企业(机构)字号</comment>
            </column>
        </property>
        <property name="nameind" type="java.lang.String">
            <column name="NAMEIND" length="30" not-null="true">
                <comment>名称行业</comment>
            </column>
        </property>
        <property name="orgform" type="java.lang.String">
            <column name="ORGFORM" length="20" not-null="true">
                <comment>组织形式</comment>
            </column>
        </property>
        <property name="tradpiny" type="java.lang.String">
            <column name="TRADPINY" length="200" not-null="true">
                <comment>字号拼音</comment>
            </column>
        </property>
        <property name="grpshform" type="java.lang.String">
            <column name="GRPSHFORM" length="30" not-null="true">
                <comment>集团简称</comment>
            </column>
        </property>
        <property name="saveperto" type="java.util.Date">
            <column name="SAVEPERTO" length="10" not-null="true">
                <comment>保留期至</comment>
            </column>
        </property>
        <set name="submitmaterial" inverse="true" cascade="save-update">
            <key>
                <column name="PRIPID" length="24" not-null="true">
                    <comment>企业ID</comment>
                </column>
            </key>
            <one-to-many class="com.cw.hibernate.entity.SubmitMaterial" />
        </set>
        <set name="trustorinfo" inverse="true" cascade="save-update">
            <key>
                <column name="PRIPID" length="24" not-null="true">
                    <comment>企业ID</comment>
                </column>
            </key>
            <one-to-many class="com.cw.hibernate.entity.TrustorInfo" />
        </set>
    </class>
</hibernate-mapping>

从表:SubmitMaterial
<hibernate-mapping>
    <class name="com.cw.hibernate.entity.SubmitMaterial" table="SUBMITMATERIAL" schema="LLF">
        <comment>提交材料表</comment>
        <id name="materialid" type="java.lang.Long">
            <column name="MATERIALID" length="10" />
            <generator class="native"></generator>
        </id>
        <property name="materialname" type="java.lang.String">
            <column name="MATERIALNAME" length="10" not-null="true">
                <comment>材料名称</comment>
            </column>
        </property>
      <many-to-one name="namereq" class="com.cw.hibernate.entity.NameREQ">
            <column name="PRIPID" not-null="true">
                <comment>企业ID</comment>
            </column>
        </many-to-one>
       </class>
</hibernate-mapping>

从表:TrustorInfo
<hibernate-mapping>
    <class name="com.cw.hibernate.entity.TrustorInfo" table="TRUSTORINFO" schema="LLF">
        <comment>被委托人信息表</comment>
        <id name="trustorid" type="java.lang.Long">
            <column name="TRUSTORID" />
            <generator class="native"></generator>
        </id>
<many-to-one name="namereq" class="com.cw.hibernate.entity.NameREQ">
            <column name="PRIPID" not-null="true">
                <comment>企业ID</comment>
            </column>
        </many-to-one>
   <!-- fetch="select"-->
        <property name="trustorname" type="java.lang.String">
            <column name="TRUSTORNAME" length="20" not-null="true">
                <comment>姓名</comment>
            </column>
        </property>
        <property name="documentname" type="java.lang.String">
            <column name="DOCUMENTNAME" length="20" not-null="true">
                <comment>证件名称</comment>
            </column>
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" length="100" not-null="true">
                <comment>通讯地址</comment>
            </column>
        </property>
        <property name="submittype" type="java.lang.String">
            <column name="SUBMITTYPE" length="20" not-null="true">
                <comment>提交方式</comment>
            </column>
        </property>
        <property name="grntime" type="java.lang.String">
            <column name="GRNTIME" length="50" not-null="true">
                <comment>准予登记所需时间</comment>
            </column>
        </property>
        <property name="trustortel" type="java.lang.String">
            <column name="TRUSTORTEL" length="20" not-null="true">
                <comment>委托人联系电话</comment>
            </column>
        </property>
        <property name="postalcode" type="java.lang.String">
            <column name="POSTALCODE" length="20" not-null="true">
                <comment>邮政编码</comment>
            </column>
        </property>
        <property name="qualifications" type="java.lang.String">
            <column name="QUALIFICATIONS" length="20" not-null="true">
                <comment>资格</comment>
            </column>
        </property>
        <property name="trustorno" type="java.lang.String">
            <column name="TRUSTORNO" length="30" not-null="true">
                <comment>证件号码</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

NameREQ实体
package com.cw.hibernate.entity;

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


/**
* NameREQ generated by MyEclipse Persistence Tools
*/

public class NameREQ  implements java.io.Serializable {


    // Fields   

     private Long pripid;
     private String entname;
     private String namedistrict;
     private String enttra;
     private String nameind;
     private String orgform;
     private String tradpiny;
     private String grpshform;
     private Date saveperto;
     private Set submitmaterial = new HashSet(0);
     private Set trustorinfo = new HashSet(0);


    // Constructors

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

/** minimal constructor */
    public NameREQ(String entname, String namedistrict, String enttra, String nameind, String orgform, String tradpiny, String grpshform, Date saveperto) {
        this.entname = entname;
        this.namedistrict = namedistrict;
        this.enttra = enttra;
        this.nameind = nameind;
        this.orgform = orgform;
        this.tradpiny = tradpiny;
        this.grpshform = grpshform;
        this.saveperto = saveperto;
    }
   
    /** full constructor */
    public NameREQ(Long pripid, String entname, String namedistrict, String enttra, String nameind, String orgform, String tradpiny, String grpshform, Date saveperto, Set submitmaterial, Set trustorinfo) {
        this.pripid = pripid;
        this.entname = entname;
        this.namedistrict = namedistrict;
        this.enttra = enttra;
        this.nameind = nameind;
        this.orgform = orgform;
        this.tradpiny = tradpiny;
        this.grpshform = grpshform;
        this.saveperto = saveperto;
        this.submitmaterial = submitmaterial;
        this.trustorinfo = trustorinfo;
    }

  
    // Property accessors

    public Long getPripid() {
        return this.pripid;
    }
   
    public void setPripid(Long pripid) {
        this.pripid = pripid;
    }

    public String getEntname() {
        return this.entname;
    }
   
    public void setEntname(String entname) {
        this.entname = entname;
    }

    public String getNamedistrict() {
        return this.namedistrict;
    }
   
    public void setNamedistrict(String namedistrict) {
        this.namedistrict = namedistrict;
    }

    public String getEnttra() {
        return this.enttra;
    }
   
    public void setEnttra(String enttra) {
        this.enttra = enttra;
    }

    public String getNameind() {
        return this.nameind;
    }
   
    public void setNameind(String nameind) {
        this.nameind = nameind;
    }

    public String getOrgform() {
        return this.orgform;
    }
   
    public void setOrgform(String orgform) {
        this.orgform = orgform;
    }

    public String getTradpiny() {
        return this.tradpiny;
    }
   
    public void setTradpiny(String tradpiny) {
        this.tradpiny = tradpiny;
    }

    public String getGrpshform() {
        return this.grpshform;
    }
   
    public void setGrpshform(String grpshform) {
        this.grpshform = grpshform;
    }

    public Date getSaveperto() {
        return this.saveperto;
    }
   
    public void setSaveperto(Date saveperto) {
        this.saveperto = saveperto;
    }

public Set getSubmitmaterial() {
return submitmaterial;
}

public void setSubmitmaterial(Set submitmaterial) {
if(submitmaterial==null)
{
submitmaterial = (Set)new SubmitMaterial();
}
this.submitmaterial = submitmaterial;
}

public Set getTrustorinfo() {
return trustorinfo;
}

public void setTrustorinfo(Set trustorinfo) {
if(trustorinfo==null)
{
trustorinfo = (Set)new TrustorInfo();
}
this.trustorinfo = trustorinfo;
}

 
}

TrustorInfo 实体:
package com.cw.hibernate.entity;

import java.util.ArrayList;
import java.util.List;



/**
* TrustorInfo generated by MyEclipse Persistence Tools
*/

public class TrustorInfo  implements java.io.Serializable {


    // Fields   

     private Long trustorid;
     private NameREQ namereq;
     private String trustorname;
     private String documentname;
     private String address;
     private String submittype;
     private String grntime;
     private String trustortel;
     private String postalcode;
     private String qualifications;
     private String trustorno;


    // Constructors

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

   
    /** full constructor */
    public TrustorInfo(NameREQ namereq, String trustorname, String documentname, String address, String submittype, String grntime, String trustortel, String postalcode, String qualifications, String trustorno) {
        this.namereq = namereq;
        this.trustorname = trustorname;
        this.documentname = documentname;
        this.address = address;
        this.submittype = submittype;
        this.grntime = grntime;
        this.trustortel = trustortel;
        this.postalcode = postalcode;
        this.qualifications = qualifications;
        this.trustorno = trustorno;
    }

  
    // Property accessors

    public Long getTrustorid() {
        return this.trustorid;
    }
   
    public void setTrustorid(Long trustorid) {
        this.trustorid = trustorid;
    }


public NameREQ getNamereq() {
return namereq;
}


public void setNamereq(NameREQ namereq) {
this.namereq = namereq;
}


public String getTrustorname() {
        return this.trustorname;
    }
   
    public void setTrustorname(String trustorname) {
        this.trustorname = trustorname;
    }

    public String getDocumentname() {
        return this.documentname;
    }
   
    public void setDocumentname(String documentname) {
        this.documentname = documentname;
    }

    public String getAddress() {
        return this.address;
    }
   
    public void setAddress(String address) {
        this.address = address;
    }

    public String getSubmittype() {
        return this.submittype;
    }
   
    public void setSubmittype(String submittype) {
        this.submittype = submittype;
    }

    public String getGrntime() {
        return this.grntime;
    }
   
    public void setGrntime(String grntime) {
        this.grntime = grntime;
    }

    public String getTrustortel() {
        return this.trustortel;
    }
   
    public void setTrustortel(String trustortel) {
        this.trustortel = trustortel;
    }

    public String getPostalcode() {
        return this.postalcode;
    }
   
    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public String getQualifications() {
        return this.qualifications;
    }
   
    public void setQualifications(String qualifications) {
        this.qualifications = qualifications;
    }

    public String getTrustorno() {
        return this.trustorno;
    }
   
    public void setTrustorno(String trustorno) {
        this.trustorno = trustorno;
    }
}

SubmitMaterial实体:
package com.cw.hibernate.entity;

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


/**
* SubmitMaterial generated by MyEclipse Persistence Tools
*/

public class SubmitMaterial  implements java.io.Serializable {


    // Fields   

     private Long materialid;
     private String materialname;
     private NameREQ namereq;


    // Constructors

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

/** minimal constructor */
    public SubmitMaterial(String materialname,NameREQ namereq) {
        this.materialname = materialname;
        this.namereq = namereq;
    }

  
    // Property accessors

    public Long getMaterialid() {
        return this.materialid;
    }
   
    public void setMaterialid(Long materialid) {
        this.materialid = materialid;
    }

    public String getMaterialname() {
        return this.materialname;
    }
   
    public void setMaterialname(String materialname) {
        this.materialname = materialname;
    }

public NameREQ getNamereq() {
return namereq;
}

public void setNamereq(NameREQ namereq) {
this.namereq = namereq;
}
}

在NameREQDAO类中添加一个方法
  public int insert(NameREQ reo,TrustorInfo info,SubmitMaterial sub)
    {
    Transaction tran = null;
    Session session = HibernateUtil.getSession();
    try{
    tran = session.beginTransaction();
    session.save(reo);
    tran.commit();
    session.close();
    return 1;
    }catch(Exception e)
    {
    e.printStackTrace();
    return 0;
    }
    }

Action中调用
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
NameREQDAO ndao = new NameREQDAO();

String EntName = request.getParameter("EntName");
String NameDistrict = request.getParameter("NameDistrict");
String EntTra = request.getParameter("EntTra");
String NameInd = request.getParameter("NameInd");
String OrgForm = request.getParameter("OrgForm");
String TradPiny = request.getParameter("TradPiny");
String GrpShForm = request.getParameter("GrpShForm");
Date SavePerTo = Timestamp.valueOf(request.getParameter("SavePerTo"));

//企业信息实体
NameREQ req = new NameREQ(EntName,NameDistrict,EntTra,NameInd,OrgForm,TradPiny,GrpShForm,SavePerTo);


String TrustorName = request.getParameter("TrustorName");
String DocumentName = request.getParameter("DocumentName");
String TrustorNo = request.getParameter("TrustorNo");
String TrustorTel = request.getParameter("TrustorTel");
String Address = request.getParameter("Address");
String PostalCode = request.getParameter("PostalCode");
String SubmitType = request.getParameter("SubmitType");
String Qualifications = request.getParameter("Qualifications");
String GRNTime = request.getParameter("GRNTime");

//企业法人,委托人实体
TrustorInfo info = new TrustorInfo(req,TrustorName,DocumentName,Address,SubmitType,GRNTime,TrustorTel,PostalCode,Qualifications,TrustorNo);
req.getTrustorinfo().add(info);
//提交材料
String[] MaterialName = request.getParameterValues("MaterialName");
String strname="";
SubmitMaterial mater=null;
for(int i=0;i<MaterialName.length;i++)
{
strname = MaterialName[i];
mater = new SubmitMaterial(strname,req);
req.getSubmitmaterial().add(mater);
}


int count = ndao.insert(req, info, mater);
if(count >0)
{
System.out.println("成功添加: "+count);
}else
{
System.out.println("添加失败");
}
return null;
}
分享到:
评论
1 楼 gyl199006 2012-10-21  
public int insert(NameREQ reo,TrustorInfo info,SubmitMaterial sub)
这个方法中的后两个参数是不是多余了呢?

相关推荐

Global site tag (gtag.js) - Google Analytics