首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate多对多级联(当中表)添加

2012-11-08 
Hibernate多对多级联(中间表)添加Hibernate多对多级联(中间表)添加SysJuese.java和SysBumen.java是多对多

Hibernate多对多级联(中间表)添加
Hibernate多对多级联(中间表)添加

SysJuese.java和SysBumen.java是多对多关系。主动权在SysBumen.java,数据库中表结果如下:
sys_bumen结构--
id       bumenmingcheng   miaoshu      zhuangtai

sys_juese结构--
id       juesemingcheng     miaoshu      zhuangtai


中间表sys_bumenjuese结构--
bumen_id         juese_id

目前sys_bumen中有一条记录--

1              采购部            。。。         0

现在想对sys_juese添加一条记录,同时在中间表sys_bumenjuese中也添加一条记录



==============================我是华丽的分割线=============================



代码如下:

SysBumen.java

package com.scm.domain;


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




/**
* SysBumen entity.
*
* @author MyEclipse Persistence Tools
*/


public class SysBumen implements java.io.Serializable {


    // Fields


    private Integer id;
    private String bumenmingcheng;
    private String miaoshu;
    private Integer zhuangtai;
    private Set sysYuangongs = new HashSet(0);
    private Set sysJueses = new HashSet(0);


    // Constructors


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


    /** minimal constructor */
    public SysBumen(String bumenmingcheng) {
       this.bumenmingcheng = bumenmingcheng;
    }


    /** full constructor */
    public SysBumen(String bumenmingcheng, String miaoshu, Integer zhuangtai,
           Set sysYuangongs, Set sysJueses) {
       this.bumenmingcheng = bumenmingcheng;
       this.miaoshu = miaoshu;
       this.zhuangtai = zhuangtai;
       this.sysYuangongs = sysYuangongs;
       this.sysJueses = sysJueses;
    }


    // Property accessors


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


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


    public String getBumenmingcheng() {
       return this.bumenmingcheng;
    }


    public void setBumenmingcheng(String bumenmingcheng) {
       this.bumenmingcheng = bumenmingcheng;
    }


    public String getMiaoshu() {
       return this.miaoshu;
    }


    public void setMiaoshu(String miaoshu) {
       this.miaoshu = miaoshu;
    }


    public Integer getZhuangtai() {
       return this.zhuangtai;
    }


    public void setZhuangtai(Integer zhuangtai) {
       this.zhuangtai = zhuangtai;
    }


    public Set getSysYuangongs() {
       return this.sysYuangongs;
    }


    public void setSysYuangongs(Set sysYuangongs) {
       this.sysYuangongs = sysYuangongs;
    }


    public Set getSysJueses() {
       return this.sysJueses;
    }


    public void setSysJueses(Set sysJueses) {
       this.sysJueses = sysJueses;
    }
}

SysBumen.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.scm.domain.SysBumen" table="sys_bumen" catalog="scm" lazy="false">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator />

        </id>
        <property name="bumenmingcheng" type="java.lang.String">

            <column name="bumenmingcheng" length="50" not-null="true" />

        </property>
        <property name="miaoshu" type="java.lang.String">

            <column name="miaoshu" length="1000" />

        </property>
        <property name="zhuangtai" type="java.lang.Integer">

            <column name="zhuangtai" />

        </property>
        <set name="sysYuangongs" inverse="true">

            <key>
                <column name="sys_id" />

            </key>
            <one-to-many />

        </set>

        <!-- 关键是这里的inverse需要设成false,cascade设成关联的级别 -->

        <set name="sysJueses" inverse="false" table="sys_bumenjuese" catalog="scm" lazy="false" cascade="save-update,delete">

            <key>
                <column name="bumen_id" not-null="true" />

            </key>
            <many-to-many entity-name="com.scm.domain.SysJuese">

                <column name="sys_id" not-null="true" />

            </many-to-many>
        </set>
    </class>
</hibernate-mapping>






SysJuese.java

package com.scm.domain;


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


/**
* SysJuese entity.
*
* @author MyEclipse Persistence Tools
*/


public class SysJuese implements java.io.Serializable {


    // Fields


    private Integer id;
    private String juesemingcheng;
    private String miaoshu;
    private Integer zhuangtai;
    private Set sysBumens = new HashSet(0);
    private Set sysYonghus = new HashSet(0);
    private Set sysQuanxians = new HashSet(0);


    // Constructors


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


    /** minimal constructor */
    public SysJuese(String juesemingcheng) {
       this.juesemingcheng = juesemingcheng;
    }


    /** full constructor */
    public SysJuese(String juesemingcheng, String miaoshu, Integer zhuangtai,
           Set sysBumens, Set sysYonghus, Set sysQuanxians) {
       this.juesemingcheng = juesemingcheng;
       this.miaoshu = miaoshu;
       this.zhuangtai = zhuangtai;
       this.sysBumens = sysBumens;
       this.sysYonghus = sysYonghus;
       this.sysQuanxians = sysQuanxians;
    }


    // Property accessors


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


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


    public String getJuesemingcheng() {
       return this.juesemingcheng;
    }


    public void setJuesemingcheng(String juesemingcheng) {
       this.juesemingcheng = juesemingcheng;
    }


    public String getMiaoshu() {
       return this.miaoshu;
    }


    public void setMiaoshu(String miaoshu) {
       this.miaoshu = miaoshu;
    }


    public Integer getZhuangtai() {
       return this.zhuangtai;
    }


    public void setZhuangtai(Integer zhuangtai) {
       this.zhuangtai = zhuangtai;
    }


    public Set getSysBumens() {
       return this.sysBumens;
    }


    public void setSysBumens(Set sysBumens) {
       this.sysBumens = sysBumens;
    }


    public Set getSysYonghus() {
       return this.sysYonghus;
    }


    public void setSysYonghus(Set sysYonghus) {
       this.sysYonghus = sysYonghus;
    }


    public Set getSysQuanxians() {
       return this.sysQuanxians;
    }


    public void setSysQuanxians(Set sysQuanxians) {
       this.sysQuanxians = sysQuanxians;
    }


}



SysJuese.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.scm.domain.SysJuese" table="sys_juese" catalog="scm">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator />

        </id>
        <property name="juesemingcheng" type="java.lang.String">

            <column name="juesemingcheng" length="50" not-null="true" />

        </property>
        <property name="miaoshu" type="java.lang.String">

            <column name="miaoshu" length="1000" />

        </property>
        <property name="zhuangtai" type="java.lang.Integer">

            <column name="zhuangtai" />

        </property>
        <set name="sysBumens" table="sys_bumenjuese" catalog="scm" lazy="false">

            <key>
                <column name="sys_id" not-null="true" />

            </key>
            <many-to-many entity-name="com.scm.domain.SysBumen">

                <column name="bumen_id" not-null="true" />

            </many-to-many>
        </set>
        <set name="sysYonghus" inverse="true">

            <key>
                <column name="sys_id" />

            </key>
            <one-to-many />

        </set>
        <set name="sysQuanxians" inverse="true" table="sys_juesequanxian" catalog="scm">

            <key>
                <column name="jueseid" not-null="true" />

            </key>
            <many-to-many entity-name="com.scm.domain.SysQuanxian">

                <column name="quanxianid" not-null="true" />

            </many-to-many>
        </set>
    </class>
</hibernate-mapping>



=================================我也是华丽的分割线===================================

遇到的错误:

org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session:

在进行更新SysBumen设置Juese集合时,Juese集合每次需要new。使用Spring管理事务之后,如果更新对象中的Juese每次不新建,会出现session重复的问题。Hibernate不能同时更新两个拥有同一id的对象。
1 楼 fengzhisha0914 2011-03-30   连引用地址都不写?

热点排行