HQL update query | HQL update | update in hibernate

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In the previous tutorials, we had a long discussion about Hibernate select query and how it works. Now we are going to discuss HQL update, delete Queries.
If we want to update the multiple records at a time, we can go with HQL update. Or else If our requirement is to update a single record then we can go with hibernate update query.
As we all know that, HQL commands are very similar to SQL Commands. Let’s compare the SQL update commands with HQL commands.
Simple HQL update: 
SQL:
 update student set marks=50 where id=10;

HQL:
 update Student s set e=s.marks=50 where s.studentId=10;
HQL update with query Parameters
SQL:
 update student set marks=50 where id=?;

HQL:
 update Student s set e=s.marks=50 where s.studentId=?;
HQL update with named Parameters
HQL:
 update Student s set e=s.marks=50 where s.studentId=sId;

To execute the non-select operations (update, delete, insert) also, we need to have the Hibernate Query object. By calling the executeUpdate() method on query object, we can fire the HQL non-select commands.
Here is the complete example for Hibernate Update.
Project Structure  :
 
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.otp.hibernate.pojo.HibernateUtility;

public class Main {

    public static void main(String[] args) {

        SessionFactory factory = HibernateUtility.getSessionFactory();

        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        // Reading complete Entity
        System.out.println("Simple update");

        String qryString = "update Student s set s.address='Hyderabad' where s.studentId=5";
        Query query = session.createQuery(qryString);
        int count = query.executeUpdate();

        System.out.println(count + " Record(s) Updated.");

        System.out.println("Updating with Query Parameters ");

        String qryString2 = "update Student s set s.address='Pune' where s.studentId=?";
        Query query2 = session.createQuery(qryString2);
        query2.setParameter(0, 1);
        query2.executeUpdate();

        int count2 = query2.executeUpdate();

        System.out.println(count2 + " Record(s) Updated.");

        System.out.println("Updating with Named Parameters");

        String qryString3 = "update Student s set s.address='Goa' where s.studentId=:sId";
        Query query3 = session.createQuery(qryString3);
        query3.setParameter("sId", 3);
        query3.executeUpdate();

        int count3 = query3.executeUpdate();

        System.out.println(count3 + " Record(s) Updated.");

        transaction.commit();
        session.clear();
        session.close();
        System.out.println("Transaction Completed !");

    }

}
The executeUpdate() method returns the rows count. By using the count, we can come to know that, how many records are updated in the database.
For all non-select operations (insert,update and delete), it is mandatory to begin and commit the transaction.
Output:
Simple update
Hibernate: update onlinetutorialspoint.student set address='Hyderabad' where id=5
1 Record(s) Updated.
Updating with Query Parameters
Hibernate: update onlinetutorialspoint.student set address='Pune' where id=?
1 Record(s) Updated.
Updating with Named Parameters
Hibernate: update onlinetutorialspoint.student set address='Goa' where id=?
1 Record(s) Updated.
Transaction Completed !

HQL delete query is as same as the update. The HQL delete is used to delete the bulk records from the database. If our requirement is to delete a single record, we can go with hibernate delete operation.
Simple HQL delete: 
SQL:
 delete from student where id=10;

HQL:
 delete from Student s  where s.studentId=10;

HQL delete with query parameters: 
SQL:
 delete from student where id=?;

HQL:
 delete from Student s  where s.studentId=?;
HQL delete with named parameters: 
HQL:
 delete from Student s  where s.studentId=:sId;

Example :

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.otp.hibernate.pojo.HibernateUtility;

public class Deleting {

    public static void main(String[] args) {

        SessionFactory factory = HibernateUtility.getSessionFactory();

        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        // Reading complete Entity
        System.out.println("Simple Delete");

        String qryString = "delete from Student s where s.studentId=1";
        Query query = session.createQuery(qryString);
        int count = query.executeUpdate();
        System.out.println(count + " Record(s) Deleted.");

        System.out.println("Deleting with Query Parameters ");

        String qryString2 = "delete from Student s where s.studentId=?";
        Query query2 = session.createQuery(qryString2);
        query2.setParameter(0, 2);
        int count2 = query2.executeUpdate();

        System.out.println(count2 + " Record(s) Deleted.");

        System.out.println("Deleting with Named Parameters");

        String qryString3 = "delete from Student s where s.studentId=:sId";
        Query query3 = session.createQuery(qryString3);
        query3.setParameter("sId", 4);
        int count3 = query3.executeUpdate();

        System.out.println(count3 + " Record(s) Deleted.");

        transaction.commit();
        session.clear();
        session.close();
        System.out.println("Transaction Completed !");

    }

}

Output :
Simple Delete
Hibernate: delete from onlinetutorialspoint.student where id=1
1 Record(s) Deleted.
Deleting with Query Parameters
Hibernate: delete from onlinetutorialspoint.student where id=?
1 Record(s) Deleted.
Deleting with Named Parameters
Hibernate: delete from onlinetutorialspoint.student where id=?
1 Record(s) Deleted.
Transaction Completed !

The complete example is available for download.
Happy learning 🙂


HQL-Update-Delete-Example

File size: 16 KB
Downloads: 958


Hibernate Filter Example Xml Configuration
hbm2ddl.auto Example in Hibernate XML Config
Different types of Object States in Hibernate
Basic Hibernate Example with XML Configuration
Hibernate Composite Key Mapping Example
Hibernate 4 Example with Annotations Mysql
Hibernate cache first level example
hibernate update query example
Hibernate Native SQL Query Example
Hibernate Named Query with Example
Hibernate Query Language (HQL) Select Operation
Difference between update vs merge in Hibernate example
Hibernate groupby criteria HQL query Example
JDBC Delete Program Example
How to Create or Delete Directories in Python ?

Hibernate Filter Example Xml Configuration
hbm2ddl.auto Example in Hibernate XML Config
Different types of Object States in Hibernate
Basic Hibernate Example with XML Configuration
Hibernate Composite Key Mapping Example
Hibernate 4 Example with Annotations Mysql
Hibernate cache first level example
hibernate update query example
Hibernate Native SQL Query Example
Hibernate Named Query with Example
Hibernate Query Language (HQL) Select Operation
Difference between update vs merge in Hibernate example
Hibernate groupby criteria HQL query Example
JDBC Delete Program Example
How to Create or Delete Directories in Python ?






marwane allay
November 23, 2018 at 3:52 pm - Reply 

thank you sir for the good exemples . god bless









kanti singh
October 20, 2020 at 1:03 pm - Reply 

Thank you so much...










marwane allay
November 23, 2018 at 3:52 pm - Reply 

thank you sir for the good exemples . god bless




thank you sir for the good exemples . god bless





kanti singh
October 20, 2020 at 1:03 pm - Reply 

Thank you so much...




Thank you so much...
 


Δ
 Hibernate – Introduction
 Hibernate – Advantages
 Hibernate – Download and Setup
 Hibernate – Sql Dialect list
 Hibernate – Helloworld – XML
 Hibernate – Install Tools in Eclipse
 Hibernate – Object States
 Hibernate – Helloworld – Annotations
 Hibernate – One to One Mapping – XML
 Hibernate – One to One Mapping foreign key – XML
 Hibernate – One To Many -XML
 Hibernate – One To Many – Annotations
 Hibernate – Many to Many Mapping – XML
 Hibernate – Many to One – XML
 Hibernate – Composite Key Mapping
 Hibernate – Named Query
 Hibernate – Native SQL Query
 Hibernate – load() vs get()
 Hibernate Criteria API with Example
 Hibernate – Restrictions
 Hibernate – Projection
 Hibernate – Query Language (HQL)
 Hibernate – Groupby Criteria HQL
 Hibernate – Orderby Criteria
 Hibernate – HQLSelect Operation
 Hibernate – HQL Update, Delete
 Hibernate – Update Query
 Hibernate – Update vs Merge
 Hibernate – Right Join
 Hibernate – Left Join
 Hibernate – Pagination
 Hibernate – Generator Classes
 Hibernate – Custom Generator
 Hibernate – Inheritance Mappings
 Hibernate – Table per Class
 Hibernate – Table per Sub Class
 Hibernate – Table per Concrete Class
 Hibernate – Table per Class  Annotations
 Hibernate – Stored Procedures
 Hibernate – @Formula Annotation
 Hibernate – Singleton SessionFactory
 Hibernate – Interceptor
 hbm2ddl.auto Example in Hibernate XML Config
 Hibernate – First Level Cache