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: Serialization and Lazy Collections
PostPosted: Thu Jun 29, 2006 10:50 am 
Newbie

Joined: Fri May 26, 2006 6:22 am
Posts: 3
I'm trying to use webservices with lazy loaded collections. This works fine except for the fact that the lazy collections get loaded when serializing the object. I don't want the collection initialized immediately, but only by calling a webmethod that reattaches the object and initializes the collection.
I guess this happens because when serializing the object, the collection property is accessed. Is there a work-around? Do I have to check whether the collections is already initialized and if not, not serialize it?

Example of mapping file:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Person" table="PERSONS">
  <id name="ID" type="Int32" column="ID">
    <generator class="identifier" />
  </id>
  <bag name="Addresses" inverse="true" lazy="true">
    <key column="Address" />
    <one-to-many class="Address" />
  </bag>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 11:32 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Does your webmethod define whether or not the actual collection is returned?

If so, I'd decouple the reference a bit more. When serializing, if you don't even need the collection, the property will be scanned of course. You can decorate the collection with an attribute that says not to serialize, but this will affect the case when you DO want the collection.

In this case, you'd have 2 webmethods, one that fetches the root object, and one that fetches the root object AND the associated collection. You'd remove the property and define a query that returns the same list.

HTH.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 1:27 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
In the case that you want the lazy collection serialized, this may help you.

http://forum.hibernate.org/viewtopic.php?t=961354

In the case that you don't want it serialized, what I've been doing is just nulling out the collection (or whatever other properties that I don't want being serialized) before returning from the web method.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 3:15 am 
Newbie

Joined: Fri May 26, 2006 6:22 am
Posts: 3
I tried using the XMLIgnore tag and this works fine when serializing the object. But then when I call a webmethod to get the collection the object gets deserialized with the collection = nothing. Nhibernate then apparently thinks the collection is already initialized, so it doesn't retrieve the data when I ask for it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 3:34 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
This is why you null the collection out when you don't want it serialized. If you use XMLIgnore, it will never serialize the collection, even when you want it to.

--
If this post helped, please be kind enough to grant me credit.


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.