-->
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.  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Unable to serialize entity due to proxy (newbie)
PostPosted: Mon Jun 05, 2006 8:55 am 
Newbie

Joined: Mon Jun 05, 2006 8:45 am
Posts: 5
Hi all,

The question looks typical but I did not find any answer. When using session.Load for instance, you get a proxied version of your entity. When trying to return the entity from a web service method, I get the error that the proxy has no default ctor and that the entity cannot be serialized.

I understand that in a good design NHibernate entities should not be sent back to the client but in simple cases, why not ? But then, how to deal with the proxy serialization problem ? For the moment I implement a DeepCopy method in all entities that I used to create a non proxied entity before returning to the client but is it the way to go ??? Any link to some more information on this topic ?

Christian


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 11:54 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
If you are serializing the entities across a web service you need to set lazy="false" anyway (so that NH does not try to lazy load properties during serialization), and that also solves the proxy issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 12:36 pm 
Newbie

Joined: Mon Jun 05, 2006 8:45 am
Posts: 5
Thanks grennis, indeed it seems to work by adding lazy="false". Now, I have a side question. How can I "tune" the use of lazy loading or not at runtime. Sometimes, I would like to use it when creating entities and sometimes not. Is it possible ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 6:06 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
What I want to know is why can't a default no arg constructor be generated for the proxy that is generated? Maybe there is something that I don't understand, but, it seems like if that could be fixed, it would be a much better solution. You may have classes that you sometimes use with web services and other times not. Having to completely disable lazy loading of a class isn't an ideal fix.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 6:25 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I wouldnt want the proxy being serialized... I only want to send my real object. Otherwise there is too much stuff happening under the covers.

I dont know the answer to if you can sometimes lazy load and sometimes not.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 12:59 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
As far as I know the proxy would be a class that is derived from your class and in the case of XML serialization, it's just serializing the public properties, so, it doesn't seem like serializing a proxy should be that big of an issue assuming it had a public no arg constructor.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 3:09 pm 
Newbie

Joined: Mon Jun 05, 2006 8:45 am
Posts: 5
Here is what I discovered :

By keeping lazy="true" but specifying a proxy attribute in the class tag of the mapping file, the class is no more proxied. Great ! But if I have a collection, then this one still gets a proxy which seems normal as it's lazy loaded ! Now I'm still trying to get rid of the proxy before returning to my client because serialization of proxied class fails.

I found that calling NHibernateUtil.Initialize(object proxy) can help to load the collection but the class remained proxied and still can't be serialized !

Please are they any guru out there that could give me some hint about how to proceed here ? This is a pretty common usage model I think.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 3:28 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
The documentation says the following about the class element lazy attribute.

Quote:
(15) lazy (optional): Setting lazy="true" is a shortcut equalivalent to specifying the name of the class itself as the proxy interface.


So, the class IS proxied if you do that.

How I got it to work was that I set lazy="false" for the class (all of them).

Note, you can still set collections for lazy or not and that seems to work fine, it's just proxying of classes that seems to have problems with XML serialization.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 5:16 pm 
Newbie

Joined: Mon Jun 05, 2006 8:45 am
Posts: 5
It looks strange. If I remember well, if I set lazy = true but do not specify proxy= at class level, I get a proxied class (not talking about collection here). If I keep lazy="true at class level but specify proxy=<the class itself>, then the class is not proxied !

Now, if I add a <many-to-one> association, specifying lazy=true and proxy at this level, the referenced class in the association is always proxied, which seems logical because it needs to control lazy loading.

After some thinking, the solution is probably to avoid serializing nhibernate managed entities but always have a separate set of entities exposed by the service and part of its data contract. Doing this is a little more work but it has the additional advantage of not exposing the db schema at service level in addition to solving the proxy problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 6:05 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I posted to the DynamicProxy wish list forum on the Castle web site about proxies not having a no arg constructor and received the following unhelpful response (basically ignored, which is the same thing that happened a while back in the NHibernate forum). If others feel like I do that the proxies should be modified to have a no arg constructor so that they can be XML serialized, please make your voice heard.

http://forum.castleproject.org/viewtopic.php?t=280


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 11, 2006 3:06 pm 
Newbie

Joined: Mon May 22, 2006 12:22 pm
Posts: 12
Hi,

I'm doing NHibernateUtil.Initialize(MyObject1), and MyObject contains a collection property, however i still get the lazy exception when i try access the collection after it has been serialized/deserialized . I made the collection class lazy="false" as suggested, but still no luck. Any help much appreciated.


MyObject class

Code:
<class name="EntityObjects.MyObject, EntityObjects" table="MyObject" >

<property name="Name" column="name" type="string"/>

<bag name="CollectionOfObject2" lazy="true" cascade="all" inverse="true access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
<key column="id"/>
<one-to-many class="EnitytObjects.MyObject2, EntityObjects">
</bag>
</class>


MyObject2 class
Code:
<class name="EntityObjects.MyObject2, EntityObjects" table="MyObject2" lazy="false" >
<property name="FullName" column="name" type="string"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 11, 2006 7:02 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
You also need to explicity initialize the collections, i.e. NHibernateUtil.Initialize(myObject.CollectionOfObject2)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:39 am 
Newbie

Joined: Mon May 22, 2006 12:22 pm
Posts: 12
Thanks, for the response. Even when doing that I still get the LazyException. However if I debug the process the values are there? - Go figure.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:55 am 
Newbie

Joined: Mon May 22, 2006 12:22 pm
Posts: 12
actually if i access a property of the collection before sending it back across the wire, the values for that collection will be there.


Example

Code:
public MyObject GetMyObject()
{
  MyObject obj = DAO.GetMyObject(id);
  int testValue = obj.MyObjectCollections.Count; //Adding this brings the values back

  return obj;
}



Looks i still have some sort of reference or proxy issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 10:29 am 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Lazy collections work fine for me. It's just lazy classes that don't work. Can you post a stack trace?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 posts ]  Go to page 1, 2  Next

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.