-->
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.  [ 7 posts ] 
Author Message
 Post subject: Serializing hibernate collection proxies to xml..
PostPosted: Sun Feb 20, 2005 12:37 pm 
Beginner
Beginner

Joined: Fri Mar 19, 2004 7:21 am
Posts: 20
Hi,
I am serializing my domain objects to xml using xstream.. Hibernate's collection proxies make my xml look ugly. ;-)

If I set lazy=false or fetch=join why does Hibernate still replace my sets with its own proxies?

Regards,
Damon.


<actions class="net.sf.hibernate.collection.Set">
<set/>
<initialized>true</initialized>
<collectionSnapshot class="net.sf.hibernate.impl.SessionImpl-CollectionEntry">
<dirty>false</dirty>
<initialized>true</initialized>
<loadedKey class="int">1</loadedKey>
<snapshot class="map"/>
<role>org.ai.core.domain.model.AdamAsset.actions</role>
</collectionSnapshot>
</actions>


Top
 Profile  
 
 Post subject: Re: Serializing hibernate collection proxies to xml..
PostPosted: Sun Feb 20, 2005 1:59 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Could you post your Hibernate mappings?

Still, you may never get the results you want. If you want your whole object graph serialized with no proxies ever, you will likely load the entire database. The only other option is to pick some spot where it does not endlessly fetch associations, and you will of necessity get a proxy there.

Or customize the serialization.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 2:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
Hibernate still replace my sets with its own proxies

Well, the short answer is that they are not proxies. They are implementations of the the collection interfaces. Big difference.

So why would it still replace your sets with its own collection implementation (cause I know thats the next question)? Because that is how it tracks entries that were added/removed and all kinds of other good things.

If you are willing to throw away all the "niceties" Hibernate's built-in collection impls give you, then you might look at using the new collection-type stuff.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 1:58 pm 
Beginner
Beginner

Joined: Fri Mar 19, 2004 7:21 am
Posts: 20
Ok thanks for the replies guys..

It is a bit of a moot point really because right now I am not using lazy but my mappings don't reflect the full relationships of the database. That is why I could get away with running automatic xml serialization.

Obviously as soon as I start putting relations into my mapping files I will need to start using lazy.

In the short term I have created a DTO and copied my data into that and then serialized it. Hacky but it gets around the fact reflection based serialization is messy.

In the longer term I would rather use customized serialization as suggested..

This got me to thinking.. hbm.xml files contains almost all the data needed to do a nice xml mapping.. Throw in a few meta tags similar to the tags for hbm2java. Castor was doing something like this at one point as well I think.

Damon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 2:11 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
damonrand wrote:
In the short term I have created a DTO and copied my data into that and then serialized it. Hacky but it gets around the fact reflection based serialization is messy.

Sounds like the right thing for now.

damonrand wrote:
In the longer term I would rather use customized serialization as suggested..

That's what I have done, but that's messy too. At least a DTO can give a clear picture of how the object has to be altered for serialization, whereas I dislike the "almost" object you get from trying to custom-rehydrate a partially serialized Hib object.

damonrand wrote:
This got me to thinking.. hbm.xml files contains almost all the data needed to do a nice xml mapping.. Throw in a few meta tags similar to the tags for hbm2java. Castor was doing something like this at one point as well I think.

Very interesting! Yes, the mapping show exactly where the associations drop off into proxies: ie, how big the default initialized object graph is. I read a while back about Christian playing with some graph delivery system and Hibernate, but never heard more about it.
Good luck!
-Michael Greer


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 7:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The next release of Hibernate3 (its already in CVS) will contain some functionality for actually rendering to XML. More precisely, it enables representing your domain model in terms of Dom4J elements. This feature is extremely experimental. It might be worth giving that a try. You'd need a custom OutputFormat that would know when to break the graph during render (or something like that); to-one associations marked as lazy are represented via proxies to the org.dom4j.Element interface, etc.

Also, fwiw, the "graph delivery system" mentioned, is the "CarrierWave" OS project. That *might* help if you still want to go down the Domain -> DTO -> XML marshalling approach.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 4:59 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
fwiw, there's some code in the xstream jira which i found helpful for marshalling hibernate persistent collections: http://jira.codehaus.org/browse/XSTR-226

there's a tiny bug in the HibernateCollectionMarshaller class tho. I had to patch as follows:

Code:
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        Object collection = source;

        if (source instanceof PersistentCollection) {
            PersistentCollection col = (PersistentCollection)source;
            col.forceInitialization();
            collection = col.getCollectionSnapshot().getSnapshot();
        }

        // the set is returned as a map by Hibernate (unclear why exactly)
        if (source instanceof PersistentSet) {
            collection = new HashSet(((HashMap)collection).values());
        }

        // delegate the collection to the approapriate converter
        if (listSetConverter.canConvert(collection.getClass())) {
            listSetConverter.marshal(collection, writer, context);
            return;
        }
        if (mapConverter.canConvert(collection.getClass())) {
            mapConverter.marshal(collection, writer, context);
            return;
        }
        if (treeMapConverter.canConvert(collection.getClass())) {
            treeMapConverter.marshal(collection, writer, context);
            return;
        }
        if (treeSetConverter.canConvert(collection.getClass())) {
            treeSetConverter.marshal(collection, writer, context);
            return;
        }

        defaultConverter.marshal(collection, writer, context);
    }


Costin had entrySet() instead of values() in the PersistentSet case. I also wonder why hibernate is returning a Map instead of a Set when you call getSnapShot() on the PersistentSet, perhaps a hibernate guru can explain?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.