Reputation: 5683
This isn't an issue per say, but I noticed something strange in my code when I do this:
Then I noticed the object is actually returned as a match on the query, even though by my reckoning the new value shouldn't have been persisted yet until I call save.
Environment is Spring JPA with Hibernate.
Can anyone explain why the object would match the query condition even though the new values haven't been persisted? Is calling a setter considered an update to the object?
(note I debugged and the object definitely doesn't match the criteria query before the values are merged, and I've triple checked, there is no other call to save the object, just setters.
Richard.
Upvotes: 0
Views: 89
Reputation: 691785
even though by my reckoning the new value shouldn't have been persisted yet until I call save
That's where you're wrong. JPA automatically persists all the changes made to managed entities. You don't need to ever call save() or merge() or anything to have the state of managed entities persistent.
JPA watches the state of these entities and, when it needs to be flushed, the modified state is transparently written to the database. This automatic flush happens before the transaction is committed or, as you just saw, before a query is executed, to make sure that the query takes into account the previous changes made on the entities.
Upvotes: 2