Zolf
asked on
Hibernate delete operation help
Hello there,
I need help to understna dand implement the delete operation in hibernate. I have these two POJO classes
1. SupplierOrder
2. SupplierOrderDetails
and I have these two hbm files which look like this. In the db I have these two tables from my hbm and I have fk from supplierOrder to SupplierOrderDetails table
now I want to implement delete operation so that when I delete a supplierorder record it should also delete its related supplierorderdetail from the db. I am new to hibernate and need how to do it and explanation for it.
I need help to understna dand implement the delete operation in hibernate. I have these two POJO classes
1. SupplierOrder
2. SupplierOrderDetails
and I have these two hbm files which look like this. In the db I have these two tables from my hbm and I have fk from supplierOrder to SupplierOrderDetails table
<hibernate-mapping>
<class name="com.kaizen.erp.shared.entities.SupplierOrder" table="supplierOrder">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="section" class="com.kaizen.erp.shared.entities.Section"
fetch="select">
<column name="sectionID" />
</many-to-one>
<many-to-one name="supplier"
class="com.kaizen.erp.shared.entities.Supplier" fetch="select">
<column name="supplierID" />
</many-to-one>
<property name="supplierOrderNo" type="string">
<column name="supplierOrderNo" />
</property>
<property name="supplierOrderCreated" type="java.util.Date">
<column name="supplierOrderCreated" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.kaizen.erp.shared.entities.SupplierOrderDetails" table="supplierOrderDetails">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="supplierOrderID" class="com.kaizen.erp.shared.entities.SupplierOrder" fetch="select">
<column name="supplierOrderID" />
</many-to-one>
<many-to-one name="productID" class="com.kaizen.erp.shared.entities.Product" fetch="select">
<column name="productID" />
</many-to-one>
<property name="orderPoint" type="java.lang.Integer">
<column name="orderPoint" />
</property>
<property name="stockCount" type="java.lang.Integer">
<column name="stockCount" />
</property>
<property name="lastMonthSales" type="java.lang.Integer">
<column name="lastMonthSales" />
</property>
<property name="avgMonthlySales" type="java.lang.Integer">
<column name="avgMonthlySales" />
</property>
<property name="avgMonthlySales_stock" type="java.lang.Integer">
<column name="avgMonthlySales_stock" />
</property>
<property name="orderQty" type="java.lang.Integer">
<column name="orderQty" />
</property>
</class>
</hibernate-mapping>
now I want to implement delete operation so that when I delete a supplierorder record it should also delete its related supplierorderdetail from the db. I am new to hibernate and need how to do it and explanation for it.
ASKER
thanks for your comments.
can you please show me the code how it will look in my case
can you please show me the code how it will look in my case
<hibernate-mapping>
<class name="com.kaizen.erp.shared.entities.SupplierOrder" table="supplierOrder">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="section" class="com.kaizen.erp.shared.entities.Section"
fetch="select">
<column name="sectionID" />
</many-to-one>
<many-to-one name="supplier"
class="com.kaizen.erp.shared.entities.Supplier" fetch="select">
<column name="supplierID" />
</many-to-one>
<property name="supplierOrderNo" type="string">
<column name="supplierOrderNo" />
</property>
<property name="supplierOrderCreated" type="java.util.Date">
<column name="supplierOrderCreated" />
</property>
<set name="supplierOrderDetails" cascade="delete-orphan" table="supplierOrderDetails" >
<key>
<column name="supplierOrderID" not-null="true" />
</key>
<one-to-many class="com.kaizen.erp.shared.entities.SupplierOrderDetails" />
</set>
</class>
</hibernate-mapping>
ASKER
is that all I need to do in my code to be able to use delete cascade. I mean when I del a SupplierOrder record it will delete its linked SupplierOrderDetails
yes.see the code written in the above link.
ASKER
<set name="supplierOrderDetails " cascade="delete" table="supplierOrderDetail s" >
this set name can be any name or it should match the variable name in the SupplierOrder POJO classes
private Set<SupplierOrderDetails> sod = new HashSet<SupplierOrderDetai ls>(0);
this set name can be any name or it should match the variable name in the SupplierOrder POJO classes
private Set<SupplierOrderDetails> sod = new HashSet<SupplierOrderDetai
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/