I'm building an audit framework for my company and am trying to grab the collection of fields which were detached along with an entity when it was removed from managed state. I need to know which fields were set to null client side -vs- which just weren't loaded when the entity was detached so that when I compare it to the managed entity I don't just blindly load the entire database. The entity has a @DetachedState field.
Does anyone know how to translate the Detached State value into a map of which fields were actually loaded on the entity?
I managed to solve this after digging through a pile of OpenJPA code. Its probably not the most elegant code and doesn't have safety checks in there that it probably should, but it gets the job done at this point.
final PersistenceCapable pc = ((PersistenceCapable) detachedEntity);
final Object[] state = (Object[]) pc.pcGetDetachedState();
final BitSet loadedFieldsOnDetach = (BitSet) state[1];
final OpenJPAEntityManager oem = getEntityManager();
final Broker _broker = ((EntityManagerImpl) oem).getBroker();
final ClassMetaData meta = _broker.getConfiguration().getMetaDataRepositoryInstance()
.getMetaData(ImplHelper.getManagedInstance(detachedEntity).getClass(), _broker.getClassLoader(), true);
for (final FieldMetaData fmd : meta.getDefinedFields()) {
if (loadedFieldsOnDetach.get(fmd.getIndex())) {
System.out.println(fmd.getName() + " was loaded on detach.");
}
}
Hope this helps someone else!