Monday, January 6, 2014

Hibernate updating rows when selecting?


Assume you have an entity that is mapped to your db with Hibernate.
when you are trying only to select this entity , you see many update sentences running in your server console.
this is a very bad thing because update may take time and to result in unwanted behavior.
please note that the updates are actually meaningless because they update the same value over and over again.

your console may result in:
select * ... from EMP
{10 rows being selected}
update  EMP set ..
update  EMP set ..
update  EMP set ..
.
.
.
10 times.
this is happening because your entity has logic of different filed in the setter.
for example:
class EMP
{
mapped..
 int a;

int b ;

getA()
grtB()

setA()
setB()
{
 setA(something);
}

because the B filed has logic with the A filed ,in B's setter the problem occurs.
this is happening because hibernate loads data into setters when performing select , and then thjins that the entity has changed because of the setB().
to fix it : remove the extra logic on A field from setB().

No comments:

Post a Comment