-->
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.  [ 5 posts ] 
Author Message
 Post subject: PersistenceDelegate for Hibernate collection classes
PostPosted: Tue Nov 16, 2004 6:00 pm 
Newbie

Joined: Tue Nov 16, 2004 1:10 pm
Posts: 5
Here is a PersistenceDelegate that converts Hibernate collections classes
into java.util collections classes. This allows them to be passed through
a java.beans.XMLEncoder. When decoded they become java.util classes.

See also HB-931.

To use, add to your Encoder the PersistenceDelegates returned by getDelegates().

This has been tested with JDK 1.4.2.

Note: XMLEncoder/XMLDecoder are not thread safe, and memory leaks
if you fail to close XMLEncoder.

Permission is granted to copy as long as copyright notice is preserved.

-------------------------------------------------------------------------------------

Code:
                                                                               
/*
* Copyright (C) 2004 Awarix, Inc. All rights reserved.
*
* $Id: HibernatePersistenceDelegate.java 1915 2004-11-11 22:50:56Z archie $
*/

package com.awarix.util.hibernate;

import java.beans.DefaultPersistenceDelegate;
import java.beans.Encoder;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;

/**
* An extension of {@link PersistenceDelegate PersistenceDelegate}
* that handles Hibernate collections classes. These classes cannot normally
* be instantiated by the application, so they don't pass successfully through
* an XML encoded stream. On the other hand, Hibernate will let you replace
* them with the usual collections classes from <code>java.util</code>. So
* that's what we do here.
*/
public class HibernatePersistenceDelegate extends DefaultPersistenceDelegate {
                                                                               
    static HibernatePersistenceDelegate INSTANCE
      = new HibernatePersistenceDelegate();

    static final Map CLASS_MAP;
    static {
        HashMap map = new HashMap();
        map.put(net.sf.hibernate.collection.Bag.class, ArrayList.class);
        map.put(net.sf.hibernate.collection.IdentifierBag.class,
          ArrayList.class);
        map.put(net.sf.hibernate.collection.List.class, ArrayList.class);
        map.put(net.sf.hibernate.collection.Map.class, HashMap.class);
        map.put(net.sf.hibernate.collection.Set.class, HashSet.class);
        map.put(net.sf.hibernate.collection.SortedMap.class, TreeMap.class);
        map.put(net.sf.hibernate.collection.SortedSet.class, TreeSet.class);
        CLASS_MAP = Collections.synchronizedMap(map);
    }

    private HibernatePersistenceDelegate() {
    }

    /**
     * Get the set of delegates to install in an
     * {@link java.beans.Encoder Encoder}
     *  to properly handle Hibernate collections types. This method returns
     * the set as a mapping from <code>Class</code> -&gt;
     * <code>PersistenceDelegate</code>.
     */
    public static Map getDelegates() {
        HashMap map = new HashMap();
        for (Iterator i = CLASS_MAP.keySet().iterator(); i.hasNext(); )
            map.put(i.next(), INSTANCE);
        return map;
    }

    /**
     * Overridden to handle conversion of Hibernate collection
     * classes into <code>java.util</code> collection classes.
     */
    protected Expression instantiate(Object oldInstance, Encoder out) {
        Class replaceType = (Class)CLASS_MAP.get(oldInstance.getClass());
        if (replaceType != null) {
            return new Expression(oldInstance,
              replaceType, "new", new Object[0]);
        }
        return super.instantiate(oldInstance, out);
    }

    /**
     * Overridden to handle conversion of Hibernate collection
     * classes into <code>java.util</code> collection classes.
     */
    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
        if (oldInstance != null && newInstance != null
          && CLASS_MAP.get(oldInstance.getClass()) == newInstance.getClass())
            return true;
        return super.mutatesTo(oldInstance, newInstance);
    }

    /**
     * Overridden to handle conversion of Hibernate collection
     * classes into <code>java.util</code> collection classes.
     */
    protected void initialize(Class type, Object oldInstance,
      Object newInstance, Encoder out) {
        Class replaceType = (Class)CLASS_MAP.get(oldInstance.getClass());
        if (replaceType != null) {
            Object replace = replace(oldInstance);
            if (type == oldInstance.getClass())
                type = replace.getClass();
            super.initialize(type, replace, newInstance, out);
        }
        super.initialize(type, oldInstance, newInstance, out);
    }

    Object replace(Object obj) {
        if (obj instanceof net.sf.hibernate.collection.Set)
            return new HashSet((Set)obj);
        else if (obj instanceof net.sf.hibernate.collection.Map)
            return new HashMap((Map)obj);
        else if (obj instanceof net.sf.hibernate.collection.SortedSet)
            return new TreeSet((SortedSet)obj);
        else if (obj instanceof net.sf.hibernate.collection.SortedMap)
            return new TreeMap((SortedMap)obj);
        else if (obj instanceof net.sf.hibernate.collection.List
          || obj instanceof net.sf.hibernate.collection.Bag
          || obj instanceof net.sf.hibernate.collection.IdentifierBag)
            return new ArrayList((Collection)obj);
        throw new RuntimeException("unexpected");
    }
}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 28, 2005 6:21 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Nice!

Thank you.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 28, 2005 8:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Thanks, could you add this to the wiki area on the website, it will get lost here in the forum.


Top
 Profile  
 
 Post subject: Not working with lazy initialized collections
PostPosted: Wed May 21, 2008 12:33 am 
Newbie

Joined: Wed May 21, 2008 12:22 am
Posts: 2
The code sample HibernatePersistenceDelegate - is not working for lazy initialized collections. And if I turned off all lazy loading then the DefaultPersistenceDelegate is able to convert objects into xml and vice versa. In that using the HibernatePersistenceDelegate only shows the Hibernate collections classes as object type of java collection classes. Unless you don't want to do that you don't need this HibernatePersistenceDelegate. Is there any solution or hint of solution to use xml encoder with hibernate lazy initialized collections?


Thanks.

Shahid


Top
 Profile  
 
 Post subject: Re: PersistenceDelegate for Hibernate collection classes
PostPosted: Wed Sep 09, 2009 6:53 pm 
Newbie

Joined: Wed Sep 09, 2009 6:49 pm
Posts: 2
Great help this class!.

Also in my case I had to add the mapping:

map.put(PersistentBag.class, ArrayList.class);

As I was using that class to map the Hibernate objects.

Cheers!


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

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.