-->
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.  [ 4 posts ] 
Author Message
 Post subject: Is there a way to get plain old object from proxy object?
PostPosted: Wed Jan 23, 2008 3:48 pm 
Newbie

Joined: Tue May 15, 2007 3:53 pm
Posts: 9
Hibernate version:
1.2.0GA

Name and version of the database you are using:
Oracle 10g

I'm building an AJAX web application with NHibernate. For the client-server communication, I'm using a library called JSON.Net (javascript object notation). For most of what I'm doing, this works fine. However, when doing a db update, like this:

Code:
Document record = (Document)session.Load(typeof(Document), id);

// make some changes to record

session.Update(record);

UpdateResponse response = new UpdateResponse();

response.Success = true;
response.Document = record;

// call JSON.Net's SerializeObject(response) to send object data back to client in text format


I'm having problems, because now the record object isn't my plain-old object, it's a DynamicProxy object, and the serializer chokes on it (self-referencing loops with the Assembly and Module information that's included).

I've tried working around this by changing this line to:

Code:
response.Document =  (Document) record;

and also
Code:
response.Document = record.Clone();


Neither of these work. (The Clone method is basically implemented as return this.MemberwiseClone()).

I've also tried just skipping the return of the updated record to the client, and forcing the client to reload the data in a separate server call, but this sometimes I get the DynamicProxy object back even without doing a db update first. It's this situation that has become the real showstopper.

So my question is, is there a way to force NHibernate to return the POCO class. Is there something similar to NHibernateUtils.GetClass that will do it for me? Or does someone have any better idea on how to not get the proxy object?

My only other solution is to re-write significant amounts of the JSON serializer, and even then I'm not sure where to begin.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 4:12 pm 
Beginner
Beginner

Joined: Tue Sep 19, 2006 11:26 am
Posts: 33
I don't think there is a way of doing what you want to do, however somebody else maybe be able to tell you otherwise. We have multi layer architecture, one layer of which is a "translation layer". The translation layer converts the NHibernate entities into serializable objects this both gets round the problem you are having, enables us to only send across the wire the data that is needed by the front-end and enables us to convert the data into a structure that is more convenient for the front-end.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 5:10 pm 
Newbie

Joined: Mon Jan 21, 2008 3:25 pm
Posts: 6
I am told this works:

Code:
public static object DeProxify (object obj)
{
    if (obj is INHibernateProxy)
    {
        LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer
            ((INHibernateProxy)obj);
        return li.GetImplementation();
    }
    return obj;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 24, 2008 2:26 pm 
Newbie

Joined: Tue May 15, 2007 3:53 pm
Posts: 9
Thanks, that has done it.


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