-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Serialization of lazy persistent objects for use across RMI
PostPosted: Tue Nov 20, 2007 1:26 pm 
Newbie

Joined: Sat Oct 07, 2006 4:57 pm
Posts: 7
I know this topic has been covered before, but I'd like to post my solution to the problem of serializing lazy persistent objects.

I wrote the following code to initialize a DeviceRepositoryObject, which is the base class for all of our persistent objects. It walks the graph and eagerly loads all objects as needed.

The initialized object can be sent to an RMI client, modified, and then sent back to the server to be saved via Hibernate. This works fine for us.

Code:
    private void initialize(DeviceRepositoryObject dro) {
      // We pass a Set to track which objects in the graph we have   processed.
        initialize(dro, new HashSet<DeviceRepositoryObject>());
    }


    private void initialize(DeviceRepositoryObject dro, Set<DeviceRepositoryObject> processed) {
        if (processed.contains(dro)) {
            return;
        }

        processed.add(dro);
        if (dro.getClass().getName().indexOf("CGLIB") != -1) {
         // For CGLIB objects, we get the implementation from the dbase
            HibernateProxy hp = (HibernateProxy) dro;
            LazyInitializer li = hp.getHibernateLazyInitializer();
         // Use the implementation to walk the graph
            dro = (DeviceRepositoryObject) li.getImplementation();
        } else if (!Hibernate.isInitialized(dro)) {
           Hibernate.initialize(dro);
        }
      // The persister has access to the child members of any DeviceRepositoryObject
        EntityPersister persister = ((SessionImpl) m_session).getEntityPersister("", dro);
       
        Object [] children = persister.getPropertyValues(dro, m_session.getEntityMode());
        for (Object child : children) {
            if (child != null) {
                Class c = Hibernate.getClass(child);
                if (DeviceRepositoryObject.class.isAssignableFrom(c)) {
                    // If its derived from DeviceRepositoryObject then recurse
                    initialize((DeviceRepositoryObject) child, processed);
                } else if (child instanceof PersistentSet) {
                    // If its a Collection mapping then recurse over members
                    PersistentSet persistentSet = (PersistentSet) child;
                    if (!Hibernate.isInitialized(persistentSet)) {
                        Hibernate.initialize(persistentSet);
                    }
                    for (Object setMember : persistentSet) {
                        initialize((DeviceRepositoryObject) setMember, processed);
                    }
                }
            }
        }
    }


If you see something wrong with this code, please post your concerns. It seems to work for me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.