Big Data and Hadoop Training @$80 or Rs. 6,000 only - valid till Oct 17th Apply Now!

Hibernate Delete Query

Ads
 
Tutorials   
What is difference between Hibernate 4 and 5? Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency 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?

Hibernate Delete Query

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

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

Ads

Ads