Hi,

Just to let you know that I have been able to finish this implementation. Here the main piece of code (very hacky!):

static Map<String, Boolean> getLoadedState(PersistenceCapable pc, Class<?> clazz) {
   try {
       BitSet loaded = null;
       if (pc.pcGetStateManager() instanceof OpenJPAStateManager) {
OpenJPAStateManager sm = (OpenJPAStateManager)pc.pcGetStateManager();
           loaded = sm.getLoaded();
       }
       // State manager may be null for some entities...
       if (loaded == null) {
           Object ds = pc.pcGetDetachedState();
           if (ds != null && ds.getClass().isArray()) {
               Object[] dsa = (Object[])ds;
               if (dsa.length > 1 && dsa[1] instanceof BitSet)
                   loaded = (BitSet)dsa[1];
           }
       }
List<String> fieldNames = new ArrayList<String>(); for (Class<?> c = clazz; c != null && PersistenceCapable.class.isAssignableFrom(c); c = c.getSuperclass()) {
           Field pcFieldNames = c.getDeclaredField("pcFieldNames");
           pcFieldNames.setAccessible(true);
fieldNames.addAll(0, Arrays.asList((String[])pcFieldNames.get(null)));
       }
Map<String, Boolean> loadedState = new HashMap<String, Boolean>();
       for (int i = 0; i < fieldNames.size(); i++)
loadedState.put(fieldNames.get(i), (loaded != null && loaded.size() > i ? loaded.get(i) : true));
       return loadedState;
   }
   catch (Exception e) {
       throw new RuntimeException("Could not get loaded state for: " + pc);
   }
}

With this Map, I can figure out what fields are uninitialized in the current bean. I'm also able to serialize the detached state to the client and to deserialize it back in the server before a merge. Uninitialized beans/collections are considered as such and I don't face the risk of unexpected updates in the database.

Thanks for your help!
Franck.

1. What is the detachment configuration settings? <property name="openjpa.DetachState" value="?"/>
I don't know. I use a vanilla WebLogic 10.3 server and deploy a standard ear test application (with EJB3 session and entity beans)...

  In your ear test application, there exists somewhere a
META-INF/persistence.xml file which defines the metadata and JPA provider's
configuration. The property configuration 'openjpa.DetachState' should be
specified in that file. The application should decide appropriate detachment
configuration (it is important for the type of use case you are describing).
The choices and its impact on how OpenJPA detach/attach mechanics will work
is documented in [1]

[1]
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_detach


I couldn't find it in 10.3 sample apps (ok, I didn't search for a long time ;-)
What is the name of this app?

Look further, you effort will be useful ;) It is in WLS 10.3. I do not
recall (or perhaps never knew) what this application is called but look for
samples on EJB3 or ask BEA/Oracle support forum.

Reply via email to