Hibernate Delete Query

In this section we will discuss how to delete record of table using HQL.


 

Ads

Tutorials   
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 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial
Ads

Hibernate Delete Query

In this section we will discuss how to delete record of table using HQL.

Hibernate Delete Query:

Delete query is used to delete the particular record of the table. Hibernate provides you multiple way  to delete records. You can delete row by using sql, HQL or by delete() method.

Example : Student is our table and in this example we are going to delete record of student whose roll number is 6.

package net.roseindia.main;

import net.roseindia.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

public class MainClass {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "DELETE FROM Student stud WHERE stud.roll=:roll";
Query query = session.createQuery(hql);
query.setParameter("roll", 6);
int rowCount = query.executeUpdate();
if (rowCount == 0) {
System.out.println("No data found to delete");
} else {
System.out.println("Your record is deleted");
}

} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
}

You can also use delete() to delete the record. Here is main class code. Just take a over view and download complete code from the given link.

package net.roseindia.main;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateDelete {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
try {

Transaction transaction = session.beginTransaction();
int roll = 2;
Object object = session.load(Student.class, roll);
session.delete(object);
transaction.commit();
System.out.println("Student record of Id=" + roll
+ " is deleted successfully");
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
}

Here is output of your code:

Hibernate: select student0_.roll_no as roll1_0_0_, student0_.course as course0_0_, student0_.name as name0_0_ from student student0_ where student0_.roll_no=?
Hibernate: delete from student where roll_no=?
Department record of Id=2 is deleted successfully

Click here to download complete source code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Delete Query

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

Advertisements

 

Discuss: Hibernate Delete Query  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads