Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate execute update with criteria

Is it possible to execute an update while using Criteria in Hibernate? For example:

Session session = getSession();
Criteria crit = session.createCriteria(User.class);
crit.add(Restrictions.eq("token", sessionToken));

User user= new User();
Transaction tx = session.getTransaction();
try 
{
    tx.begin();
    session.updateWithCriteria(user, crit); //my imaginary function 
    tx.commit();
}
catch (Exception e) 
{
    e.printStackTrace();
    tx.rollback();
}

session.close();
like image 475
vlio20 Avatar asked Sep 12 '14 22:09

vlio20


People also ask

How to update an object in hibernate?

We can update an object in hibernate by calling the update () method, provided by the org.hibernate.Session. Though the update () method is used to update an object, there are two different ways to use update () method. Without loading an object from the database Loading an object from the database

How to add restrictions for criteria query in hibernate?

Here in hibernate, we need to use add () method available for the Criteria object and it is helped to add restrictions for a criteria query. It will have all the comparison operations such as >,<,=, between, etc. System.out.println ("Listing the geekEmployee data whose salary lesser than 50000");

How to filter the day by criteria in hibernate?

In SQL, we will filter the day by means of adding the “WHERE” clause. Here in hibernate, we need to use add () method available for the Criteria object and it is helped to add restrictions for a criteria query. It will have all the comparison operations such as >,<,=, between, etc.

What are the new criteriaupdate and criteriadelete interfaces?

The new CriteriaUpdate and CriteriaDelete interfaces add the missing bulk update and delete operations to the Criteria API. From my point of view this is a small but great enhancement that allows us to use the Criteria API in even more situations. I like it!


2 Answers

There is a very powerful feature called:

15.4. DML-style operations

small cite from doc:

... However, Hibernate provides methods for bulk SQL-style DML statement execution that is performed through the Hibernate Query Language...

So, while this is not about criteria - we still can use our domain model for querying, because it is about HQL. This is a snippet showing the power:

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
        .setString( "newName", newName )
        .setString( "oldName", oldName )
        .executeUpdate();
tx.commit();
session.close();

SUMMARY: Having that in place:

  • we can use query to filter results
  • we can apply bulk update on it
  • we won't need to load these rows in memory, into the session...
like image 105
Radim Köhler Avatar answered Oct 04 '22 12:10

Radim Köhler


Now we can do something like this for bulk update and delete. New api's released for criteriaUpdate and CriteriaDelete

CriteriaBuilder cb = this.em.getCriteriaBuilder();
// create update
CriteriaUpdate<Order> update = cb.createCriteriaUpdate(Order.class);
// set the root class
Root e = update.from(Order.class);
// set update and where clause
update.set("amount", newAmount);
update.where(cb.greaterThanOrEqualTo(e.get("amount"), oldAmount));
// perform update
this.em.createQuery(update).executeUpdate();
like image 38
Payal Bansal Avatar answered Oct 04 '22 13:10

Payal Bansal