How to change Hibernate´s auto persistance strategy

Posted by Kristofer Borgstrom on Stack Overflow See other posts from Stack Overflow or by Kristofer Borgstrom
Published on 2010-05-21T20:00:12Z Indexed on 2010/05/21 20:20 UTC
Read the original article Hit count: 214

Filed under:
|
|

I just noted that my hibernate entities are automatically persisted to the database (or at least to cache) before I call any save() or update() method. To me this is a pretty strange default behavior but ok, as long as I can disable it, it´s fine.

The problem I have is I want to update my entity´s state (from 1 to 2) only if the entity in the database still has the state it had when I retrieved [1] (this is to eliminate concurrency issues when another server is updating this same object). For this reason I have created a custom NamedQuery that will only update the entity if state is 1. So here is some pseudo-code:

//Get the entity
Entity item = dao.getEntity(); 
item.getState(); //==1

//Update the entity
item.setState(2); //Here is the problem, this effectively changes the state of my entity braking my query that verifies that state is still == 1.

dao.customUpdate(item); //Returns 0 rows changes since state != 1.

So, how do I make sure the setters don´t change the state in cache/db?

Thanks, Kristofer

© Stack Overflow or respective owner

Related posts about jpa

Related posts about hibernate