现在需要将一个多对多的关系拆分,比如学生和老师之间的关系,拆成两个多对一,并且中间表也需要实体类、配置文件。我大概是这样做的:
学生表:
<hibernate-mapping default-lazy="false">
    <class name="Student" table="t_stutent">
        <id name="id" type="long">
            <column name="ID" precision="20" scale="0"/>
            <generator class="sequence">
                <param name="sequence">SEQ_STUTENT</param>
            </generator>
        </id>
        <set name="Teacher" inverse="true" lazy="false" cascade="all-delete-orphan">
            <key column="STUTENT_ID"/>
            <one-to-many class="StudentTeacher"/>
        </set>
    </class>
</hibernate-mapping>教师表:
<hibernate-mapping default-lazy="false">
    <class name="Teacher" table="t_teacher">
        <id name="id" type="long">
            <column name="ID" precision="20" scale="0"/>
            <generator class="sequence">
                <param name="sequence">SEQ_TEACHER</param>
            </generator>
        </id>
        <set name="stutent" inverse="true" lazy="false" cascade="all-delete-orphan">
            <key column="TEACHER_ID"/>
            <one-to-many class="StutentTeacher"/>
        </set>
    </class>
</hibernate-mapping>中间表(即StutentTeacher类对应的表):
<hibernate-mapping default-lazy="false">
    <class name="StutentTeacher" table="t_stu_tea">
        <id name="id" type="long">
            <column name="ID" precision="20" scale="0"/>
            <generator class="sequence">
                <param name="sequence">SEQ_STU_TEA</param>
            </generator>
        </id>
        <property name="studentId" type="long">
            <column name="STUTENT_ID" precision="20"/>
        </property>
        <many-to-one name="stutent" class="Stutent"  fetch="select" insert="false" update="false">
            <column name="STUTENT_ID"/>
        </many-to-one>
        <property name="teacherId" type="long">
            <column name="TEACHER_ID" precision="20"/>
        </property>
        <many-to-one name="teacher" class="Teacher" insert="false" update="false" fetch="select">
            <column name="TEACHER_ID"/>
        </many-to-one>
    </class>
</hibernate-mapping>现在问题是在保存、删除的时候,没有什么问题,中间表中的记录都已经写入了。但是在前端做更新操作时有问题,举例说明,比如stutent1中保存了teacher1,我在前端更新stutent1中为teacher2,这时没问题;但当我更新后希望的是stutent1中保存teacher1和teacher2,这时候就有问题了,它会报:
“org.springframework.orm.hibernate3.HibernateSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Stutent.teacher; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Stutent.teacher
Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Stutent.teacher”哪位高手帮我看一下好吧?多谢了!

解决方案 »

  1.   

    <many-to-one name="stutent" class="Stutent"  fetch="select" insert="false" update="false"> 
    <many-to-one name="teacher" class="Teacher" insert="false" update="false" fetch="select">
    中的
    insert="false" update="false" 改下试试
      

  2.   

    孤立的数据清除
    http://hi.baidu.com/landor2004/blog/item/d7bc760193d995031d95839a.html
      

  3.   

    是不是这个
    学生表的
    <set name="Teacher" inverse="true" lazy="false" cascade="all-delete-orphan"> 改成teacher看映射文件基本都是生成的了,好久没看了
      

  4.   

    你把 cascade="all-delete-orphan"删掉看下
      

  5.   

    "比如stutent1中保存了teacher1,我在前端更新stutent1中为teacher2,这时没问题;但当我更新后希望的是stutent1中保存teacher1和teacher2"
    你想通过更新student1的teacher1为teacher2
    同时数据库保存这个学生有两个老师信息?
    这怎么能是更新完成的呢?更新也只是更新对应的一条啊,应该是添加吧?
    楼主的业务不是太清楚……这只是我的一点儿理解!
      

  6.   

    你的意思是s1的t1改成t2没问题,新增t2却有问题?
      

  7.   

    简单点,t和s分别管好自己,然后中间表管理t和s的联系,两个many2one搞定
      

  8.   

    把中间表在程序当中去掉!角色表<set name="popmenus" table="rolemidpopmenus" lazy="false">
    <key>
    <column name="roleid" unique="true" />
    </key>
    <many-to-many class="com.ipanel.pojo.Popmenus"
    column="popmenusid" />
    </set>权限菜单表<set name="roles" table="rolemidpopmenus" inverse="true" lazy="false">
                <key>
                    <column name="popmenusid" not-null="true" />
                </key>
                <many-to-many class="com.ipanel.pojo.Role" column="roleid"/>
            </set>
      

  9.   

    通过hibernate工具生成拆分两上一对多关系 是比较完美的方式 但操作上来比较繁琐