Reputation: 10273
I know that JPA implementors (like Hibernate) monitor the entities attached to the persistence context, and if any field of any entity is changed, then the change is updated to the database.
My question is, how does JPA keep track of the changes? The Entities are not intercepted (by proxyies), so for sure it is not throught the interception of the "setters", how is it done?
I am interested on the implementation detail of this "dirty checking" feature.
Upvotes: 3
Views: 6261
Reputation: 140
Note that in the meantime hibernate has expanded its dirty checking options. Hibernate now also supports byte code instrumentation and custom dirty checking options. See this link for more info on these newer options.
Upvotes: 0
Reputation: 15577
They do something that is totally implementation dependent (i.e JPA spec doesn't help you one bit here).
Some implementations (e.g DataNucleus, OpenJPA) use bytecode enhancement and are able to efficiently manage what fields are dirty since loading (and have no need of keeping a snapshot of the original field values).
Other implementations have to compare with some value from earlier (or via a comparison with the datastore).
Upvotes: 8
Reputation: 691625
They make a snapshot of the loaded entities at load time, keep these snapshots in memory, and compare, at flush time, the snapshot of the entities with their current state. If the state differs, the entity must be updated. If they still match, it must not.
Upvotes: 5