Reputation: 3103
This is the table of customer history and these are sales activities, now customer made a payment of $ 200, which apply on the following ID
5 - Balance After Apply = 0
7 - Balance After Apply = 0
8 - Balance After Apply = 0
10 - Balance After Apply = 85.9
How we can do in hibernate that it get all row (only rows in which balance apply) update accordingly ?
Upvotes: 2
Views: 59
Reputation: 92140
I'm not sure to understand your question but you can try something that looks like:
public void updateBalance(float newBalance,List<String> ids) {
String hql = "update UserHistory set balance= :newBalance where id in (:ids)";
Query query = session.createQuery(hql);
query.setFloat("newBalance",newBalance);
query.setParameterList("ids",ids);
int rowCount = query.executeUpdate();
System.out.println("Rows affected: " + rowCount);
}
updateBalance(0,Arrays.asList("5","7","8"));
updateBalance(85.9,Arrays.asList("10"));
This works fine if you have to set the same balance on multiple rows.
If each row gets potentially a new balance, then you can get the rows using the Criteria API using something like that:
Restrictions.in("id", Arrays.asList("5","7","8","10"));
and once you have your entities, you can simply update their balance and save them.
Upvotes: 1