Hibernate Many to One Mapping Delete Query Example

Hibernate » on Jul 25, 2011 { 4 Comments } By Sivateja

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]
  • [child pojo]
  • [our logic]
  • Vendor.hbm.xml
  • Customer.hbm.xml
  • hibernate.cfg.xml

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


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


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="str.Vendor" table="vendor">

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



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

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



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

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></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="">update</property>

<mapping resource="Customer.hbm.xml"></mapping>
<mapping resource="Vendor.hbm.xml"></mapping>

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();

		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();

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



You Might Also Like

  ::. About the 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.


4 Responses to “Hibernate Many to One Mapping Delete Query Example”
  1. siva says:

    Dear sir,
    while running the “Hibernate Many to One Mapping Delete Query Example” i am getting follow Exceptions, Kindly check it and update…

    Hibernate: select customer0_.CUSTOMERID as CUSTOMERID1_0_, customer0_.CUSTOMERNAME as CUSTOMER2_1_0_, customer0_.Vdummy as Vdummy1_0_ from CUSTOMER customer0_ where customer0_.CUSTOMERID=?
    Hibernate: select vendor0_.VENDORID as VENDORID0_0_, vendor0_.VENDORNAME as VENDORNAME0_0_ from VENDOR vendor0_ where vendor0_.VENDORID=?
    Hibernate: delete from CUSTOMER where CUSTOMERID=?
    Hibernate: delete from VENDOR where VENDORID=?
    Exception in thread “main” org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

    … 16 more lines [ Deleted some lines in your comment by Support team ]

  2. Java4s says:


    Regarding to your

    Exception in thread “main” org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update.

    Actually its our mistake, happened while typing…

    See In our live example Customer.hbm.xml line number 11 we have given some thing like..

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

    we need to include the class name with which it has to do the relation right ?

    please change the above line to_

    <many-to-one name="parentObjects" column="Vdummy" class="str.Vendor" cascade="all" />

    And clear the old database tables and run the application.

    Always download the example and try for safe side..! just don’t copy paste, by mistake there may be these type of simple bugs 🙂

  3. Prabhakar says:

    I am following your tutorial it’s very nice. In Many to One example instated of saving single customer save TWO customers and try delete one customer from the table it’s throwing exception how can I fix that one.Thank you very much.

  4. rahul says:

    add one line c.setVdummy(null); before session.delete(c); to remove exception and delete the record


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.