Hibernate Delete Query

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

Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1?



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 (
DeleteHQLExample.java), 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: DeleteHQLExample.java 

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
 * http://www.roseindia.net 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


Share on Google+Share on Google+

Hibernate Delete Query

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



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 sourceforge.net 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....