Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate update query

Tags:

java

hibernate

Any one tell me the HQL for this SQL code

UPDATE ModelClassname SET ClassVariablename=ClassVariablename+10 WHERE ClassVariableId=001;

like image 912
Débora Avatar asked Sep 28 '11 14:09

Débora


1 Answers

There is no point using HQL to do that, you can use direct SQL if you want to do that, through a JDBC query (or even through a Hibernate query, you can use SQL queries).

Using HQL queries to update is only recommended when doing batch updates, not a single row. http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html#batch-direct

A more object-oriented way would be to load your object using HQL, do what you need to do in the Java world (columnValue +=10, whatever else you need to do), and then persist it back using hibernate session flush.

I suppose it involves more operations so it's less efficient (in pure performance) but depending on your Hibernate configuration (caching, clustering, second-level cache, etc.) it could be a lot better. Not to mention more testable, of course.

like image 196
Guillaume Avatar answered Nov 04 '22 14:11

Guillaume