父母被删除时,而不是孩子正在删除其更新 [英] Rather than child is deleting its updating ,when parent is deleted

查看:160
本文介绍了父母被删除时,而不是孩子正在删除其更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我删除父,子不会被删除,而不是删除子,子正在更新。父表是员工和子表是 EmployeeProject 员工和项目之间存在一对多的关系一个员工有很多项目我已经完成了请检查我在哪里误认这是查询显示在控制台上

  Hibernate:update employee_project set employeeNumber = null where employeeNumber =? 
Hibernate:从employee中删除EMPLOYEE_NUMBER =?

这是删除的逻辑

  public boolean deleteEmployee(Employee employee){
Transaction transaction = null;
布尔标志;
尝试{
transaction = session.beginTransaction();
session.delete(employee);
transaction.commit();
flag = true;
catch(HibernateException异常){
if(transaction!= null)
transaction.rollback();
flag = false;
}
返回标志;





这是父表映射文件
Employee.hbm.xml

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD // EN
http://www.hibernate.org/dtd/hibernate-mapping- 3.0.dtd>

< class name =Employeetable =employee>
< meta attribute =class-description>
此类包含员工详细信息
< / meta>
< id name =employeeNumbertype =intcolumn =EMPLOYEE_NUMBER>

< / id>
< property name =firstNametype =stringcolumn =FIRST_NAME>< / property>
< property name =lastNametype =stringcolumn =LAST_NAME>< / property>
< set name =employeeProjectscascade =all-delete-orphanlazy =false
inverse =true>
< key column =employeeNumber/>
< / set>
< property name =address1type =stringcolumn =ADDRESS_1>< / property>

< / class>

< / hibernate-mapping>

这是子表映射文件
project.hbm.xml

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD // EN
http://www.hibernate.org/dtd/hibernate-mapping- 3.0.dtd>

< class name =EmployeeProjecttable =employee_project>
< meta attribute =class-description>
此类包含员工详细信息
< / meta>
< composite-id>
column =EMPLOYEE_NUMBER>< / key-property>
< / composite-id>
< property name =roletype =stringcolumn =PROJECT_ROLE>< / property>
< / class>
< / hibernate-mapping>


解决方案

在employee.hbm.xml中, =true并将级联修改为cascade =all。这里把inverse =true表示这个关系的所有者是EmployeeProject,而不是Employee,它不是我们想要的。

  <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =com.nousinfo.tutorial.model.Employeetable =EMPLOYEE>
< id type =intname =employeeIdcolumn =EMPLOYEE_ID/>
< set name =employeeProjectscascade =all>
< key column =EMPLOYEE_ID/>
< / set>
< / class>
< / hibernate-mapping>

我还修改了键列的名称以匹配EmployeeProject的composite-id中的名称,否则您将有2列具有相同的信息。
在project.hbm.xml中,修改多对一,如下所示,以获得正确的双向映射:

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =com.nousinfo.tutorial.model.EmployeeProjecttable =EMPLOYEE_PROJECT>
< composite-id>
< key-property type =intname =employeeIdcolumn =EMPLOYEE_ID/>
< key-property type =stringname =projectCodecolumn =PROJECT_CODE/>
< / composite-id>
< / class>
< / hibernate-mapping>


My problem is when i delete the parent, child is not deleted but instead of deleting child ,child is updating.Parent table is Employee and child table is EmployeeProject there is one-to-many relation ship exist between employee and project one employee had many projects what i have done please check where i m mistaking this is the query is showing on console

Hibernate: update employee_project set employeeNumber=null where employeeNumber=?
Hibernate: delete from employee where EMPLOYEE_NUMBER=?

this is the logic of delete

 public boolean deleteEmployee(Employee employee) {
        Transaction transaction = null;
        boolean flag;
        try {
            transaction = session.beginTransaction();
            session.delete(employee);
            transaction.commit();
            flag = true;
        } catch (HibernateException exception) {
            if (transaction != null)
                transaction.rollback();
            flag = false;
        }
        return flag;
    }

This is parent table mapping file Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.nousinfo.tutorial.model">
    <class name="Employee" table="employee">
        <meta attribute="class-description">
            This class contains the employee detail
        </meta>
        <id name="employeeNumber" type="int" column="EMPLOYEE_NUMBER">

        </id>
        <property name="firstName" type="string" column="FIRST_NAME"></property>
        <property name="lastName" type="string" column="LAST_NAME"></property>
        <set name="employeeProjects" cascade="all-delete-orphan" lazy="false"
            inverse="true">
            <key column="employeeNumber" />
            <one-to-many class="com.nousinfo.tutorial.model.EmployeeProject" />
        </set>
        <property name="address1" type="string" column="ADDRESS_1"></property>

    </class>

</hibernate-mapping>

This is child table mapping file project.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.nousinfo.tutorial.model">
    <class name="EmployeeProject" table="employee_project">
        <meta attribute="class-description">
            This class contains the employee detail
        </meta>
        <composite-id>
            <key-property name="employeeNumber" type="int"
                column="EMPLOYEE_NUMBER"></key-property>
            <key-property name="projectCode" type="string" column="PROJECT_CODE"></key-property>
        </composite-id>
        <property name="startDate" type="date" column="START_DATE"></property>
        <property name="endDate" type="date" column="END_DATE"></property>
        <property name="role" type="string" column="PROJECT_ROLE"></property>
        <many-to-one name="employee" class="com.nousinfo.tutorial.model.Employee" ></many-to-one>
    </class>
</hibernate-mapping>

解决方案

In employee.hbm.xml, remove the flag inverse="true" and modify the cascading to cascade="all". Putting inverse="true" here means that the owner of this relation is EmployeeProject and not Employee which is not what we want.

<?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">
<hibernate-mapping>
    <class name="com.nousinfo.tutorial.model.Employee" table="EMPLOYEE">
        <id type="int" name="employeeId" column="EMPLOYEE_ID"/>
        <set name="employeeProjects" cascade="all">
            <key column="EMPLOYEE_ID"/>
            <one-to-many class="com.nousinfo.tutorial.model.EmployeeProject"/>
        </set>
    </class>
</hibernate-mapping>

I also modified the name of the key column to match the one in EmployeeProject composite-id otherwise you will have 2 columns with the same information. In project.hbm.xml, modify the many-to-one as follows to have a correct bidirectional mapping :

<?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">
<hibernate-mapping>
    <class name="com.nousinfo.tutorial.model.EmployeeProject" table="EMPLOYEE_PROJECT">
        <composite-id>
            <key-property type="int" name="employeeId" column="EMPLOYEE_ID"/>
            <key-property type="string" name="projectCode" column="PROJECT_CODE"/>
        </composite-id>
        <many-to-one name="employee" column="EMPLOYEE_ID" insert="false" update="false"/>
    </class>
</hibernate-mapping>

这篇关于父母被删除时,而不是孩子正在删除其更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆