Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating JPA-Hibernate entity without selecting it from database

I have a hibernate entity called Execution. It is created in the beginning of my process and updated at end, indicating how it has finished.

I would like to update a single property of this entity, without causing a select in my database.

Execution execution = entityManager.getReference(Execution.class, executionId); execution.setStatus(Status.FINISHED); //--> Calling this method fires a SELECT in my Database. I didn't want it to happen, I just want to update my entity.

This is not specific to this method, any other method called results in a SELECT clause. In fact, the select appears to happen even before my method is called. My impression is that hibernate proxies put some code inside my class no-args contructor to fire a select everytime any method is called.

Is it possible to update JPA/Hibernate entities without firing a SELECT statement in my database?

like image 908
user266391 Avatar asked Feb 25 '10 13:02

user266391


1 Answers

This is how Hibernate works. Its proxy object loads the real object from DB whenever any non-id property is accessed.

Try saving/loading the object at the beginning of your process (to execute that SELECT), and make sure that the session does not auto-flush whenever an object is touched (I think the default behaviour is no auto-flush, but it is worth a check).

Or you could also try detaching your object from the Hibernate session during processing.

like image 154
Péter Török Avatar answered Oct 21 '22 13:10

Péter Török