Embedded class problems

2 Messages Forum Options Options
Permalink
rgarret
Embedded class problems
Reply Threaded More
Print post
Permalink
Hello everyone,

I am trying to implement audit logging using lifecycle callbacks. In my PostUpdate callback method I extract the StateManager from the PersistenceCapable object I'm passed and from there I iterate through the fields using the dirty BitSet from the StateManager. I log the field's old value (extracted using the fetchInitialField method) and the fields new value (using the fetchField method) along with some other metadata about the database columns, table, etc...

As I am iterating through the dirty fields if I come accross an Embedded PersistanceCapable object I try to recursively process it. I am able to extract the StateManager from the Embedded instance but the dirty BitSet indicates that all the fields are dirty even though I've only manipulated a few of them. Also, I am not able to get the initial values using the fetchInitialField method because the SaveFieldManager of the StateManager is null. I'm about out of ideas. Any help is much appreciated

Thanks,
-Reece

Pinaki Poddar
Re: Embedded class problems
Reply Threaded More
Print post
Permalink
Hi,
   1.  Is this using runtime or build-time enhancement?
   2.  May be related to this JIRA issue? [1]

           http://issues.apache.org/jira/browse/OPENJPA-659

   3. The code snippet below runs through fields of an instance to its dirty embedded instance's dirty fields. Works as expected on latest OpenJPA.
   
        @PostUpdate
        public void changed() {
                EntityManager em = OpenJPAPersistence.getEntityManager(this);
                PersistenceCapable pc = ImplHelper.toPersistenceCapable(this, em);
                OpenJPAStateManager sm = (OpenJPAStateManager)pc.pcGetStateManager();
                ClassMetaData meta = sm.getMetaData();
                BitSet dirty = sm.getDirty();
                for (int i=0; i<dirty.size(); i++) {
                        if (dirty.get(i)) {
                                if (meta.getField(i).isEmbedded()) {
                                        Object embedded = sm.fetch(i);
                                        PersistenceCapable epc = ImplHelper.toPersistenceCapable(embedded, em);
                                        OpenJPAStateManager esm = (OpenJPAStateManager)epc.pcGetStateManager();
                                        BitSet edirty = esm.getDirty();
                                        for (int j=0; j<edirty.size(); j++) {
                                                if (edirty.get(j))
                                                  System.err.println(esm.getMetaData().getField(j));
                                        }
                                }
                        }
                }
               
        }