开发者

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 possi开发者_C百科ble to update JPA/Hibernate entities without firing a SELECT statement in my database?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜