HQL Update Query hibernate
Hibernate Query language also can be use to update data in the table. To update table query pattern is almost same as native SQL and only difference is: in place of table name you will have to use class name in HQL.
Note: This class is extension of previous tutorial. If you are interested to see full working example with all set up please refer this tutorial Hibernate One To Many Annotation Example
Please have java class example below:
package com.javahonk; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.javahonk.util.HibernateUtil; public class HQLSelect { static SessionFactory sf = HibernateUtil.getSessionFactory(); public static void main(String[] args) { Session session = sf.openSession(); session.beginTransaction(); System.out.println("Update query example \n"); Query query = session.createSQLQuery("update Mother set " + "first_Name = :firstName, last_Name = :lastName" + " where mother_Id = :motherId"); query.setString("firstName", "Change"); query.setString("lastName", "Name"); query.setInteger("motherId", 3); int noOfRowsUpdated = query.executeUpdate(); System.out.println("No of rows updated: "+noOfRowsUpdated); session.getTransaction().commit(); session.close(); } }
For more information about HQL please read this hibernate official tutorial