-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: SQL Bulk Update with dynamic where conditions
PostPosted: Tue Oct 31, 2017 7:52 am 
Newbie

Joined: Tue Oct 31, 2017 7:44 am
Posts: 1
Hi Hibernate Experts,

My requirement is to be able to do a bulk update similar to:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

- The WHERE condition is dynamic - from 1 to N conditions ANDed together.
- Values would be parameterized.

Is this possible with Hibernate OR Hibernate Search?
Please advise.

Thanks,
Kapil


Top
 Profile  
 
 Post subject: Re: SQL Bulk Update with dynamic where conditions
PostPosted: Tue Oct 31, 2017 9:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Sure, it is possible.

Just use the JPA CriteriaUpdate for that:

Code:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<Post> q = cb.createCriteriaUpdate(Post.class);
Root<Post> root = q.from(Post.class);
q.set(root.get("valid"), true)
.where(root.get("id").in(Arrays.asList(1, 2, 3)));

int result = em.createQuery(q).executeUpdate();


Top
 Profile  
 
 Post subject: Re: SQL Bulk Update with dynamic where conditions
PostPosted: Thu Dec 07, 2017 1:04 am 
Newbie

Joined: Wed Feb 22, 2017 4:35 pm
Posts: 19
Hey Vlad, is it possible to do this kind of thing with criteriaUpdate?

Code:
explain analyze update stuff set thing = CASE
   WHEN (id = 34769) THEN 1
   WHEN (id = 34770) THEN 2
   WHEN (id = 35222) THEN 3
   WHEN (id = 35222) THEN 4
   END
   where id = any(values (34769),(34770),(35222),(35223));


Top
 Profile  
 
 Post subject: Re: SQL Bulk Update with dynamic where conditions
PostPosted: Thu Dec 07, 2017 5:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Criteria API supports CASE and ANY for sub queries.

VALUES is not supported, but you could just use an IN clause for that which is also supported.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.