Is it possible to detach Hibernate entity, so that changes to object are not automatically saved to database?

newbie picture newbie · Apr 27, 2011 · Viewed 59k times · Source

I have Hibernate entity that I have to convert to JSON, and I have to translate some values in entity, but when I translate values, these values are instantly saved to database, but I don't want to save these changes to database. Is there any workaround for this problem?

Answer

axtavt picture axtavt · Apr 27, 2011

You can detach an entity by calling Session.evict().

Other options are create a defensive copy of your entity before translation of values, or use a DTO instead of the entity in that code. I think these options are more elegant since they don't couple conversion to JSON and persistence layer.