-->
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.  [ 12 posts ] 
Author Message
 Post subject: Elegant way to serialize Hibernate objects to XML?
PostPosted: Sun Aug 31, 2003 8:18 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
I'm wondering whether any Hibernate users have found (or written) a good framework for serializing Hibernate objects to XML. I'm working on an app that uses XML/XSLT for presentation, and currently, I am writing my own code to iterate through the various Hibernate objects and add elements/attributes to my view XML document. there must be a better way to do this...Anyone have experience with Apache's Betwixt? It looks like it might do the trick, though it seems a bit immature. I've read through the docs but haven't had time to actually sit down and learn it. Perhaps there are other alternatives?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 9:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can have a look at Hibernate Databinder. It uses Hibernate metadata to serialize hibernate objects to XML. But this is not really a heavily used feature and noone ever paid much attention to it.


Top
 Profile  
 
 Post subject: Cool!
PostPosted: Tue Sep 02, 2003 11:50 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
I've been playing around with Databinder and its very cool, I'm a bit suprised there hasn't been more interest, this seems like it would be very useful. I did find one thing a bit perplexing though, the way that Databinder deals with lazy collections. The only way I can get Databinder to pull collection objects is with setInitializeLazy(true). This is fine if you want to pull in ALL collections of an object, but I've got lots of objects with multiple collections and I don't want to pull a huge tree of objects to build my view.

I'd like to selectively add child elements from my Hibernate collections. If I use Hibernate.initialize(cat.getKittens()), before binding the object to the XML document, Databinder correctly sees that the collection is initialized but doesn't pull the objects. Is this just a limitation of Databinder or am I missing something?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 6:13 pm 
Newbie

Joined: Sat Aug 30, 2003 12:37 pm
Posts: 16
There are other alternatives to Betwixt. I have been using Castor XML for a while and it gets the job done. I would suggest that you look into the following:



Castor is considered to be *production quality* where as Jibx is supposed to be the fastest. You can read about XML-Java Data binding performace at

http://www-106.ibm.com/developerworks/l ... atabdopt2/

I hope this help,

Slug


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 6:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Databinder correctly sees that the collection is initialized but doesn't pull the objects. Is this just a limitation of Databinder or am I missing something?



Ah Databinder is supposed to include any initialized collections in the output. I'm not sure if this is broken. I thought it worked. Brad Clow was the last person working on this.


Check the source.


Top
 Profile  
 
 Post subject: Maybe a bug?
PostPosted: Wed Sep 03, 2003 10:54 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
Hrmmm, I think there's an issue here...but its pretty subtle. Here's my test snippet:

User user = (User) userlist.get(0);
//Hibernate.initialize(user.getTeams());
//db.setInitializeLazy(true);
db.bind(user);
System.out.println(db.toXML());

Here's the output with no collections initialized:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-custom datetime="03 September 2003 07:30:33">
<User id="N400004">
<userid>15656</userid>

<username>aaa</username>

<pass>aaa</pass>

<age>33</age>

<category id="N400031">809</category>

<!--elements of collection signups-->
<!--end of elements-->
<!--elements of collection results-->
<!--end of elements-->
<!--elements of collection teams-->
<!--end of elements-->
<!--elements of collection races-->
<!--end of elements-->
</User>

<Category id="N400031">
<categoryid>809</categoryid>

<categorydesc />

<categoryname>test</categoryname>

<!--elements of collection teams-->
<!--end of elements-->
<!--elements of collection results-->
<!--end of elements-->
<!--elements of collection users-->
<!--end of elements-->
</Category>
</hibernate-custom>


Here's the output with Hibernate.initialize(user.getTeams());
<?xml version="1.0" encoding="UTF-8"?>

<hibernate-generic datetime="03 September 2003 06:42:32">
<object class="User" package="com.hib">
<uid name="userid" type="integer">15656</uid>
<property name="username" type="string">aaa</property>
<property name="pass" type="string">aaa</property>
<property name="age" type="integer">33</property>
<property name="category" uid="809" class="Category" package="com.hib" type="com.hib.Category"/>
<collection name="signups" class="java.util.Set" element-type="com.hib.Signup" lazy="uninitialized"/>
<collection name="results" class="java.util.Set" element-type="com.hib.Result" lazy="uninitialized"/>
<collection name="teams" class="java.util.Set" element-type="com.hib.Team" lazy="initialized"/>
<collection name="races" class="java.util.Set" element-type="com.hib.Race" lazy="uninitialized"/>
</object>
<object class="Category" package="com.hib">
<uid name="categoryid" type="integer">809</uid>
<property name="categorydesc" type="string"></property>
<property name="categoryname" type="string">test</property>
<collection name="teams" class="java.util.Set" element-type="com.hib.Team" lazy="uninitialized"/>
<collection name="results" class="java.util.Set" element-type="com.hib.Result" lazy="uninitialized"/>
<collection name="users" class="java.util.Set" element-type="com.hib.User" lazy="uninitialized"/>
</object>
</hibernate-generic>

As I step through the code, the renderProperty method correctly sees that the Teams set is initialized, correctly identifies it as a set, but for some reason thinks the set is null, so it doesn't add any elements to the document.

I think the problem stems from the fact that the User object and its associated Category object both have their own 0-n relationships to Teams. I tried serializing other object types and Databinder picked up the collections OK (although the default output format doesn't format the collection objects as child elements...bleh). Anyway, lemme know if you want this in JIRA.

My mappings:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.hib.User"
table="users"
>
<id
name="userid"
type="int"
column="userid"
>
<generator class="hilo"> <param name="table">hi_value</param> <param name="column">next_value</param> <param name="max_lo">100</param> </generator>
</id>
<property
name="username"
type="java.lang.String"
column="username"
not-null="true"
unique="true"
/>
<property
name="pass"
type="java.lang.String"
column="pass"
not-null="true"
/>
<property
name="age"
type="int"
column="age"
length="4"
/>

<!-- associations -->
<!-- bi-directional many-to-one association to Category -->
<many-to-one
name="category"
class="com.hib.Category"
not-null="true"
>
<column name="categoryid" />
</many-to-one>

<set
name="signups"
lazy="true"
inverse="true"
>
<key>
<column name="userid" />
</key>
<one-to-many
class="com.hib.Signup"
/>
</set>

<set
name="results"
lazy="true"
inverse="true"
>
<key>
<column name="userid" />
</key>
<one-to-many
class="com.hib.Result"
/>
</set>
<!-- bi-directional one-to-many association to Userteam -->

<set name="teams" table="userteam" lazy="true">
<key column="userid"/>
<many-to-many class="com.hib.Team" column="teamid"/>
</set>

<set name="races" table="result" lazy="true">
<key column="userid"/>
<many-to-many class="com.hib.Race" column="raceid"/>
</set>

</class>
</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.hib.Category"
table="category"
>
<id
name="categoryid"
type="int"
column="categoryid"
>
<generator class="hilo"> <param name="table">hi_value</param> <param name="column">next_value</param> <param name="max_lo">100</param> </generator>
</id>
<property
name="categorydesc"
type="java.lang.String"
column="categorydesc"
length="18"
/>
<property
name="categoryname"
type="java.lang.String"
column="categoryname"
not-null="true"
unique="true"
/>

<!-- associations -->

<set name="teams" table="categoryteams" lazy="true">
<key column="categoryid"/>
<many-to-many class="com.hib.Team" column="teamid"/>
</set>

<!-- bi-directional one-to-many association to Result -->
<set
name="results"
lazy="true"
inverse="true"
>
<key>
<column name="categoryid" />
</key>
<one-to-many
class="com.hib.Result"
/>
</set>

<!-- bi-directional one-to-many association to User -->
<set
name="users"
lazy="true"
inverse="true"
>
<key>
<column name="categoryid" />
</key>
<one-to-many
class="com.hib.User"
/>
</set>

</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<class
name="com.hib.Team"
table="teams"
>
<id
name="teamid"
type="int"
column="teamid"
>
<generator class="hilo"> <param name="table">hi_value</param> <param name="column">next_value</param> <param name="max_lo">100</param> </generator>
</id>
<property
name="teamname"
type="java.lang.String"
column="teamname"
not-null="true"
unique="true"
/>
<property
name="teamdesc"
type="java.lang.String"
column="teamdesc"
/>
<property
name="minage"
type="int"
column="minage"
length="4"
/>
<property
name="maxage"
type="int"
column="maxage"
length="4"
/>

<set name="categories" table="categoryteams" lazy="true">
<key column="teamid"/>
<many-to-many class="com.hib.Category" column="categoryid"/>
</set>

<!-- bi-directional one-to-many association to Result -->
<set
name="results"
lazy="true"
inverse="true"
>
<key>
<column name="teamid" />
</key>
<one-to-many
class="com.hib.Result"
/>
</set>

<!-- bi-directional one-to-many association to Userteam -->
<set name="users" table="userteam" lazy="true">
<key column="teamid"/>
<many-to-many class="com.hib.User" column="userid"/>
</set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Just to reiterate...
PostPosted: Wed Sep 03, 2003 11:04 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
And just to reiterate, stepping through with .setInitializeLazy(true) Databinder correctly added the Users collection (along with a huge tree of other stuff, which is why I don't want to do this). Strange....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 11:07 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
#@(!@&@#!!! I mean the Teams collection (I.e. user.getTeams())...How do you edit previous posts on this system?????


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 9:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
submit some code to JIRA to help reproduce this.

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 3:26 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
slug wrote:
There are other alternatives to Betwixt. I have been using Castor XML for a while and it gets the job done. I would suggest that you look into the following:


Castor is considered to be *production quality* where as Jibx is supposed to be the fastest. You can read about XML-Java Data binding performace at

http://www-106.ibm.com/developerworks/l ... atabdopt2/

I hope this help,

Slug


I have been trying to determine if there is someway or anyone who has had Castor XML generate its java bean classes annotated with hibernate xdoclet tags. We are trying to generate from a very large XML schema (hundreds of files) and this would really save some time.

Anyone?


Top
 Profile  
 
 Post subject: Castor and xDoclet
PostPosted: Thu Jan 29, 2004 9:58 am 
Newbie

Joined: Mon Sep 22, 2003 5:43 am
Posts: 15
http://xdoclet.sourceforge.net/tags/exolab-tags.html

I am using hibernate with Castor to generate XML dumps of various parts of a database.

I am having a great deal of difficulty "importing" xml documents. As I export the hibernate objects I strip all hibernate surrogate ids. Leaving only my "business" identities. But as I import the xml files I have to hook up each object created with the database. At this point I get net.sf.hibernate.NonUniqueObjectException messages.

Has anyone done anything similar? It seems quite easy: export to XML make a few changes and then update the db from the xml. And if so an example would be much appreciated.

thanks
Dan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 12:06 am 
Newbie

Joined: Tue Mar 23, 2004 1:04 am
Posts: 3
Hi
One moment unclear for me. How to deserialize persistent object from XML using detabinder? I see it serves only for serialization, is where easy way to deserialize object?


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