java - How does Spring Data jpa know properties changed? -
so lookup object repository. if save object after lookup, spring data smart enough not update database. if change property within object , save, spring data update. how know needs update or not?
this not provided spring data, feature of persistence framework (hibernate, openjpa, eclipselink,...).
persistence providers enhance domain objects "stuff" optimization. normally, done called runtime enhancement, class gets loaded inside of application , enhanced there(runtime weaving).
openjpa allows build-time-enhancement, means, "openjpa-domain-extension-stuff" becomes added entities @ compile time. (there maven goal in openjpa plugin too) https://openjpa.apache.org/builds/2.2.2/apache-openjpa/docs/ref_guide_pc_enhance.html
if run mvn openjpa:enhance simple domain following: (i used jad decompile class, long show stuff inside, copied relevant parts)
import org.apache.openjpa.enhance.*; import org.apache.openjpa.util.intid; import org.apache.openjpa.util.internalexception; public class entity implements persistencecapable { public integer getid() { return pcgetid(this); } public void setid(integer id) { pcsetid(this, id); } .... .... private static final void pcsetid(entity entity, integer integer) { if(entity.pcstatemanager == null) { entity.id = integer; return; } else { entity.pcstatemanager.settingobjectfield(entity, pcinheritedfieldcount + 3, entity.id, integer, 0); return; } } .... protected void pcclearfields() { id = null; } public persistencecapable pcnewinstance(statemanager statemanager, object obj, boolean flag) { entity entity = new entity(); if(flag) entity.pcclearfields(); entity.pcstatemanager = statemanager; entity.pccopykeyfieldsfromobjectid(obj); return entity; } }
by manipulating entity, pcstatemanager gets invoked. if run persist operation, persistence framework checks statemanager if there changes within entity , sends update database if necessary.
Comments
Post a Comment