Home Hibernate Hibernate Delete Query
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Hibernate Delete Query

Advertisement
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 (
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 
Configuration
().configure().buildSessionFactory();
  sess = fact.openSession();
  String hql = "delete from 
Insurance insurance where id = 2"
;
  Query query = sess.createQuery(hql);
  int row = query.executeUpdate();
  if (row == 0){
  System.out.println("Doesn'
t deleted any row!"
);
  }
  else{
  System.out.println("Deleted
 Row: " 
+ row);
  }
  sess.close();
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  }
  }
}

Download this code.

Output:

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

Advertisement

Liked it!  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.

Posted on: July 30, 2007

Ask Questions?    Discuss: Hibernate Delete Query   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Mukesh
August 28, 2011
DATABASE Creation

Thanks for the code but you have not given how to design the database accordingly which may be difficult for beginner.
Ramya
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.
Ramya
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.
RUSHO SHAW
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();
Brandice
December 13, 2011
WNoghavkgDHibuRk

This infroatmion is off the hizool!
kumar
March 21, 2013
error

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.
hjc
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()); } }
jubinpatel
March 1, 2012
error

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
quality

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
Manoj
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....
DMCA.com