hibernate hql delete query example


hibernate hql delete query example

Let’s take simple hibernate hql delete query example , so that we can understand how to update database record using Hibernate Query Language.

  1. Employee table in oracle DB
  2. Employee.java
  3. Employee.hbm.xml
  4. hibernate.cfg.xml
  5. Run.java

Employee.java

package com.javagf.bean;
public class Employee { 
 private int id;
 private String name;
 private Integer salary;
 public Integer getSalary() {
   return salary; 
 }
 public void setSalary(Integer salary) {
   this.salary = salary; 
 }
 public int getId() {
   return id;
 }
 public void setId(int id) { 
   this.id = id;
 }
 public String getName() {
   return name; 
}
 public void setName(String name) {
   this.name = name;
 }
}

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 18 Jun, 2017 2:16:54 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping> <class name="com.javagf.bean.Employee" table="EMPLOYEE">
 <id name="id" type="int"> 
     <generator class="assigned" />
 </id>
 <property name="name" type="java.lang.String"></property>
 <property name="salary" type="int"></property>
 </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
   <property name="hbm2ddl.auto">update</property>
   <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
   <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
   <property name="connection.username">system</property> 
   <property name="connection.password">manager</property>
   <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <mapping resource="com/javagf/bean/Employee.hbm.xml" />
 </session-factory>
</hibernate-configuration>

Run.java

 package com.javagf.test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Run {
 public static void main(String[] args) {
   Configuration cfg = new Configuration();
   cfg.configure("com/javagf/configuration/hibernate.cfg.xml");
   SessionFactory factory = cfg.buildSessionFactory();
   Session session = factory.openSession();
   Transaction transaction = session.beginTransaction();
   Query query = session.createQuery("delete Employee where id = :id");
   query.setParameter("id", 101);
   int counter = query.executeUpdate();
   transaction.commit();
   System.out.println(counter + " Record Updated");
   session.close(); 
 }
}

Here above example will delete single record.

How to delete all record

package com.javagf.test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Run {
 public static void main(String[] args) {
  Configuration cfg = new Configuration();
  cfg.configure("com/javagf/configuration/hibernate.cfg.xml");
  SessionFactory factory = cfg.buildSessionFactory(); 
  Session session = factory.openSession(); 
  Transaction transaction = session.beginTransaction();
   Query query = session.createQuery("delete Employee");
   int counter = query.executeUpdate();
   transaction.commit();
   System.out.println(counter + " Record Updated");
    session.close();
  }

Related Posts.

 






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229