views:

164

answers:

1

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?

A: 

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.

Péter Török
I'll accept this answer as the correct one. I came across an easier solution to what I want, by using JPA-QL Bulk Update/Insert http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/batch.html#batch-direct
@user266391 I am not familiar with JPA so thanks for the link :-)
Péter Török