0

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>
henrycharles
  • 1,019
  • 6
  • 28
  • 66

2 Answers2

0

change your cascade from all-delete-orphan to delete

because all you need is that when the parent gets deleted all the related children should be removed as well. That is what cascade=delete does.

cascade=all-delete-orphan is used to delete children entity as well, it certainly serves different scenario. It will delete children entities when the relationship is cut offFor instance,if child entity is removed from the collection in parent entity and then child will be deleted when hibernate session is closed. This cascade type is useful when child entity logically cannot exist without a parent.

spiritwalker
  • 2,257
  • 14
  • 9
  • i have tried by changing to delete but only parent is deleted not the child can u tell me if i don't override the hash() and equals() method then does it make difference? cause i didn't override those on my pojo class – henrycharles Feb 15 '13 at 13:28
  • one more question in delete logic as mentioned above i didn't add child to collection i just simply pass employee object then without adding child it removes the child record from database ,i have tried all,all,delete-orphan,delete none of them is working i don't know where the problem is occuring – henrycharles Feb 15 '13 at 13:34
  • right before you delete **employee**, can you confirm if there is any **project** in the collection in **employee**. – spiritwalker Feb 15 '13 at 14:13
  • DB **CRUD** operations don't care **hashCode** or **equals**. – spiritwalker Feb 15 '13 at 14:14
0

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>
overmeulen
  • 1,158
  • 6
  • 15
  • thanks for replying but its not working the same query is running – henrycharles Feb 19 '13 at 08:27
  • really the exact same query ? It's not possible since I changed the key column name ... – overmeulen Feb 19 '13 at 09:45
  • i have done exactly same thing but now employee is also not deleting now only this query is running update employee_project set EMPLOYEE_NUMBER=null where EMPLOYEE_NUMBER=? i don't know why its happening i stuck with this problem form the past 4 days i m not able to resolve this – henrycharles Feb 19 '13 at 09:57
  • I edited my answer with a simplified version of your problem (I removed the properties). I tested it and it works, the delete is cascaded to the class EmployeeProject. – overmeulen Feb 19 '13 at 12:39