Share on Google+Share on Google+

Hibernate Delete Query

In this lesson we will show how to delete rows from the underlying database using the hibernate.

Hibernate Delete Query


In this lesson we will show how to delete rows from the underlying database using the hibernate. Lets first write a java class to delete a row from the database.

Create a java class:
Here is the code of our java file (, which we will delete a row from the insurance table using the query "delete from Insurance insurance where id = 2"

Here is the code of delete query: 

package roseindia.tutorial.hibernate;

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

public class DeleteHQLExample {
 @author vinod Kumar
 * Hibernate
 Criteria Query Example

  public static void main(String[] args) {
  // TODO Auto-generated method stub  
  Session sess = null;
  try {
  SessionFactory fact = new 
  sess = fact.openSession();
  String hql = "delete from 
Insurance insurance where id = 2"
  Query query = sess.createQuery(hql);
  int row = query.executeUpdate();
  if (row == 0){
t deleted any row!"
 Row: " 
+ row);
  catch(Exception e){

Download this code.


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: delete from insurance where ID=2

Deleted Row: 1



Posted on: July 30, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Delete Query   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 19, 2011
Regd Hibernate Delete Query Example

Hi, The tutorial was good as i am a beginner with Hibernate. I tried the delete query example and i am not getting the expected output. The output comes as insurance has not been mapped. In the program too, there is no object formation of insurance table. Can you please explain me how this process works.. Thanks, Ramya.
December 1, 2011
Delete one row using hibernate without query

Instead of using the query we can use the delete method in org.hibernate.Session In the above example, to delete the row in insurance with id=2, we may use the following: Transaction tran = sess.beginTransaction(); Insurance insurance = (Insurance)sess.get(Insurance.class, 2); sess.delete(insurance); tran.commit();
December 13, 2011

This infroatmion is off the hizool!
March 21, 2013

in this delete class, int row = query.executeUpdate(); line was showing the error like "The method executeUpdate() is undefined for the type Query" can you clear my query.
January 13, 2012
Modification to code

Downloaded latest hibernate.jar from public static void main(String[] args) { // TODO Auto-generated method stub Session sess = null; try { SessionFactory fact = new Configuration().configure().buildSessionFactory(); sess = fact.openSession(); Transaction transaction = sess.beginTransaction(); String hql = "delete from roseindia.tutorial.hibernate.Insurance where ID = 3"; Query query = sess.createQuery(hql); int row = query.executeUpdate(); transaction.commit(); if (row == 0){ System.out.println("Doesn't deleted any row!"); } else{ System.out.println("Deleted Row: " + row); } sess.flush(); sess.close(); } catch(Exception e){ System.out.println(e.getMessage()); } }
March 1, 2012

pls change the all hibernate stating code because they all are worked properly but they dont take effect in database so again make some changes here one more thing is for note: <!--delete from Insurance insurance where id = 2"; --> here First "Insurance" is use the name of your "bean name" Other is "insurance" is use the name of your database "table name" <!-- changed code --> public class FirstExample extends Contact{ public static void main(String[] args) { Session session = null; Transaction transaction = null ; try{ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); transaction.begin(); String hql = "delete from Contact contact where id = 7"; Query query = session.createQuery(hql); int row = query.executeUpdate(); if (row == 0){ System.out.println("Doesn't deleted any row!"); } else{ System.out.println("Deleted Row: " + row); } transaction.commit(); session.flush(); session.close(); } catch(Exception e){ System.out.println("error in first"+e.getMessage()); } } }
Jose -Galeano
August 27, 2012

your examples has errors or some lines of code are not present for a good execution for example do not has commit and transaction definition in several of them. The above example give me exception = null. Please revise the examples and your correct execution. Thanks
December 18, 2012
It is not using hibernate method to delete record

Hello Sir, It is using SQL query to delete record. It is not the hibernate way to delete record. As SQL query would make it database dependent. Please update the code.
the legend
April 9, 2013
hai i like your website.....

it's very useful to us for web development....