Hibernate Many to One Mapping Delete Query Example

Let us see the example on hibernate many to one delete query…

  • If we delete child, parent will not deleted because, it may have lot of other child objects
  • In many to one relationship, when ever a child object is deleted then its parent object is also deleted, provided if  that parent object has no other child objects, means if parent has only one child, in this case if we delete child, parent will also got deleted, but in all other cases it will throws exception

files required..

  • Vendor [parent pojo]
  • Customer.java [child pojo]
  • OurLogic.java [our logic]
  • Vendor.hbm.xml
  • Customer.hbm.xml
  • hibernate.cfg.xml

Vendor.java

123456789101112131415161718192021package str;

public class Vendor {

	private int vendorId;
	private String vendorName;

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

}

Customer.java

1234567891011121314151617181920212223242526package str;
public class Customer {

	private int customerId;
	private String customerName;
	private Vendor parentObjects;

	public Vendor getParentObjects() {
		return parentObjects;
	}
	public void setParentObjects(Vendor parentObjects) {
		this.parentObjects = parentObjects;
	}
	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;
	}
}

Vendor.hbm.xml

12345678910111213<?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"/>

</class>
</hibernate-mapping>

Customer.hbm.xml

1234567891011121314<?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"/>

<many-to-one name="parentObjects" column="Vdummy" cascade="all" />

</class> </hibernate-mapping>

hibernate.cfg.xml

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

OurLogic.java

12345678910111213141516171819202122232425262728293031package 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(Customer.class, new Integer(506));
		Customer c = (Customer)o;		

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

		    session.close();
		    System.out.println("many to one delete done..!!");
		    factory.close();       

	}
}

Related Articles

post a comment