[Solved] Entity out of sync after persist/insert

Hi there, I’m currently upgrading my server dependencies and migrate from Hibernate 3.* to 5.4.3. Im facing a problem now I did’t had before and I’m wondering what’s happening.

I’m getting the: StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
And I’m pretty sure it’s no concurrency problem.

I’m using Mysql 5.7.20 and use IDENTITY for my IDs, where I think could be the root of the problem.

What I do is, that i start a session(EntityManager), create a new entity (called JobState) and persist it in a first transaction (the insert statement is done immediatly). After running my job I receave the entity again by find(id) in a second transaction, what works without a select statement (still the same session) and it is even containig the generated ID as expected. Fine so far. Now when I change the JobState, what causes an update statement on the commit of the second transaction, I run into the StaleObjectStateException. But why?

It seems that the entity became out of sync after the insert, cause when I force a select by calling refresh on the entity before updating it works fine.

Is this an intended behaviour and im just getting it wrong? Or is it kind of a bug?


Finally I discovered the real problem somewhere else. I use a Date(@Temporal.Timestamp) as @Version and somehow Hibernate seems to became more strict in checking the Timestamp. My MySql field did not store the milliseconds, but the entity, hold by the entitymanager, had the milliseconds set from the innitial java.util.Date. Therefor the entity timestamp and the db record were different, what caused a version conflict and led to the StaleObjectStateException.