Solved

Hibernate delete operation help

Posted on 2014-01-27
7
298 Views
Last Modified: 2014-01-27
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

<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>

Open in new window



<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>

Open in new window


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.
0
Comment
Question by:zolf
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 39811387
you can do that using cascase option in hbm file.check this link.

http://www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/
0
 

Author Comment

by:zolf
ID: 39811398
thanks for your comments.

can you please show me the code how it will look in my case
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 39811408
<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>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:zolf
ID: 39811422
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
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 39811442
yes.see the code written in the above link.
0
 

Author Comment

by:zolf
ID: 39811455
<set name="supplierOrderDetails" cascade="delete" table="supplierOrderDetails" >

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<SupplierOrderDetails>(0);
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 39811580
it should match the variable name in the SupplierOrder POJO
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now