JPA dirty checking

梦想的初衷 提交于 2019-12-10 10:17:37

问题


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.


回答1:


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).




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8507922/jpa-dirty-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!