-->
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.  [ 8 posts ] 
Author Message
 Post subject: XML Serialization into existing objects
PostPosted: Mon Jun 19, 2006 3:55 pm 
Newbie

Joined: Sat Sep 03, 2005 4:10 am
Posts: 9
Hibernate version:1.0.2

I'm new to nhibernate (and c#), but am ploughing through. I could do with some advice regarding xml serialization and nhibernate.

I want to be able to take a xml document and deserialise it into a set of objects and then persist these with nhibernate. however, if the root object already exists (ie a key value is the same) i want to update this object (and it's children) rather than just adding a new set of objects.

what's the best way to do this? is there a way? or am i barking up the wrong tree completely.

thanks,
mike.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 4:43 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i was reading this over the weekend:

http://www.hibernate.org/368.html

haven't tried it yet, though.

-devon


Top
 Profile  
 
 Post subject: XML Serialization into existing objects
PostPosted: Tue Jun 20, 2006 2:38 am 
Newbie

Joined: Sat Sep 03, 2005 4:10 am
Posts: 9
hmm, looks interesting (and complicated!). if i read it right though, i think this solution is to create an xml property type rather than deserialising from xml to .net objects and then nhibernating them. i shall go through it in some more detail...

mike.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 11:46 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
actually, it doesn't look too complicated. one caveat is that all properties rturn the type XmlData and we know that sometimes we want an "int" or a "bool" or a "string" and so there must be some mechanism to cast these without having to cast a property every time you use it. or maybe i'm missing something...

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 12:22 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I haven't tried using XmlSerializer directly, but, in theory it should work just liked it does with ASP.NET XML web services which uses XmlSerializer for serializing/deserializing data.

Basically, "it just works". You just call ISession.SaveOrUpdate() and pass it the object graph that you want to save to the database. NHibernate checks the key value for the object(s). If it's not set to the "unsaved value," then, it does an UPDATE, otherwise it does an INSERT.

One potential problem you might run into though is that XmlSerializer doesn't handle circular references (as far as I know). If you have bidirectional relationships between objects you will.

BinaryFormatter and SoapFormatter are supposed to be able to handle circular references. SoapFormatter has the disadvantage that it doesn't handle generics (as far as I know).

Also, if you're using ASP.NET web services, you can used RPC encoding instead of the default if you need circular reference support. However, this is possibly at the cost of compatibility. Not sure what ASP.NET is doing under the covers to acheive this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 1:51 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
One potential problem you might run into though is that XmlSerializer doesn't handle circular references (as far as I know). If you have bidirectional relationships between objects you will.

this is true. in this instance, if you didn't want to use the binary formatter, you use the XmlInclude attribute to include a reference to the object contained within the collection. kind of a hack because you end up with all kinds of XmlInclude references (you have to hit all objects in the graph even if they're four levels deep).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 6:48 pm 
Newbie

Joined: Sat Sep 03, 2005 4:10 am
Posts: 9
ok, so i receive and deserialise the xml. I have a reference, but it is not the actual id field (this is internal to nhibernate and i don't want to pass it around0. however, i can get the object using reference and this gives me the id. i then put this in the object and it's child objects and then isession.saveorupdate. it seems a bit messy, but it should work?

mike.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:02 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I'm not sure I understand you. If what you're saying is that you don't want the id values being XML serialized, then, I don't think that will work. NHibernate needs the id values in order to know which object is which and whether it's a new object that should be "saved" or an existing object that should be "updated" (based on whether the id value is set to the "unsaved value" which is normally null or 0 assuming you're using an auto-increment id).


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