Newsletter

Hibernate One to Many Mapping Delete Query Example

Hibernate » on Jul 22, 2011 { 6 Comments } By Sivateja

Let us see the logic for hibernate one to many mapping delete query, Actually every thing is same like Hibernate One-to-Many Mapping Insert  but only change is in OurLogic.java file.

But mates, ensure you came through these sessions for better understand

Files required..

  • Vendor.java [pojo class]
  • Customer.java [pojo class]
  • OurLogic.java
  • Customer.hbm.xml
  • hibernate.cfg.xml
  • Vendor.hbm.xml

Vendor.java

package str;

import java.util.Set;

public class Vendor {

private int vendorId;
private String vendorName;
private Set children;

public int getVendorId() {
return vendorId;
}
public void setVendorId(int vendorId) {
this.vendorId = vendorId;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}

}

Customer.java

package str;
public class Customer {

private int customerId;
private String customerName;
private int forevenId;

public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public int getForevenId() {
return forevenId;
}
public void setForevenId(int forevenId) {
this.forevenId = forevenId;
}

}

Customer.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Customer" table="customer">

<id name="customerId" column="custid"  />
<property name="customerName" column="custname" length="10"/>

<property name="forevenId" column="forevenid"  insert="false" />

</class>
</hibernate-mapping>

Vendor.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Vendor" table="vendor">

<id name="vendorId" column="vendid"  />
<property name="vendorName" column="vendname" length="10"/>

<set name="children" cascade="all" >

<key column="forevenid" />
<one-to-many class="str.Customer" />

</set>

</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="Customer.hbm.xml"></mapping>
<mapping resource="Vendor.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

Deleting Single Parent Object With All Child

OurLogic.java

package str;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class OurLogic {

public static void main(String args[])
{

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();

Object o = session.get(Vendor.class, new Integer(101));
Vendor v = (Vendor)o;

Transaction tx = session.beginTransaction();
session.delete(v);
tx.commit();

session.close();
System.out.println("One To Many is Done for deleting..!!");
factory.close();

}
}

If we want to delete all the parent objects will all its corresponding child objects., then the logic will be like..

Deleting All Parent Objects With All Childs

OurLogic.java

package str;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class OurLogic {

public static void main(String args[])
{

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();

Query qry =session.createQuery("from Vendor v");
List l=qry.list();
Iterator it = l.iterator();

Transaction tx = session.beginTransaction();

while(it.hasNext())
{

Object o = it.next();
Vendor v = (Vendor) o;
session.delete(v);
}

tx.commit();

session.close();
System.out.println("One To Many is Done for deleting all parents with childs...!");
factory.close();

}
}

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

6 Responses to “Hibernate One to Many Mapping Delete Query Example”
  1. chandra says:

    can you please give detailed tables on other relations also .

  2. thayeef says:

    Hi,
    Very nice tutorial .Thanks alot.

  3. karthick says:

    shall we use …..
    createQuery(“delete from Vendor v”); instead of above query…

    pls reply… 🙂

  4. Gourav Bohra says:

    Shall we use …..
    createQuery(“delete from Vendor v”); instead of above query…

    pls reply… 🙂

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.