-->
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.  [ 2 posts ] 
Author Message
 Post subject: using hibernate to migrate data between datasources
PostPosted: Wed Apr 20, 2011 10:48 am 
Newbie

Joined: Wed Apr 20, 2011 10:20 am
Posts: 2
Hello,

I am trying to use hibernate to migrate data between two databases. The approach I was thinking was basically to have a hbm.xml mapping file to map the source data to a POJO, within the context of a "source" database hibernate session, then use the same POJO to map to a different hbm.xml file associated with a "destination" database hibernate session.

Based on documentation and reading through some forum posts, it sounds like I would want to do a procedure like:

1. start new "source" hibernate session
2. load the rows for a database table into a collection (my dataset is small)
3. close the hibernate session, so that the objects become "detached"
4. start a new "destination" hibernate session, start transaction
5. save the items from the collection ("session.save(eachItem)"). (I know there are facilities for batch saving items, but i want to get it working first, and then worry about optimizing.)
6. commit transaction

One of the objects has a <list> of children. On both the "source" and "destination" hbm.xml, it is configured as such (names generalized for simplicity):
Code:
<list name="items" table="items" lazy="false"  cascade="all">
         <key column="parentid" not-null="true" />
         <list-index column="index" />
         <composite-element class="foo.bar.Parent$Item">
            <property name="amount" column="amount" type="long" />
               <type name="org.hibernate.type.EnumType">
                  <param name="enumClass">foo.bar.ItemType</param>
                  <param name="type">12</param>
               </type>
            </property>
            <property name="period" column="period">
               <type name="org.hibernate.type.EnumType">
                  <param name="enumClass">foo.bar.ItemPeriod</param>
                  <param name="type">12</param>
               </type>
            </property>
         </composite-element>
      </list>


(There are other parts of the hbm.xml files where other parts of the mapping differ, which is why I am not using the same file on both sides.)

What is happening is that the <list> of items is not getting saved to the destination database.

Could someone be so kind as to point out what I am doing wrong? Perhaps my approach is not optimal?

Cheers,

Alex


Top
 Profile  
 
 Post subject: Re: using hibernate to migrate data between datasources
PostPosted: Wed Apr 20, 2011 2:14 pm 
Newbie

Joined: Wed Apr 20, 2011 10:20 am
Posts: 2
I found the cause, per this sentence in the Hibernate documentation:
Quote:
"Two entities cannot share a reference to the same collection instance."


I get why that is the case under normal, single session use cases. I suppose I was under the impression that the proxy classes would be removed once the session was closed, or if one evict()'ed the object from a session, but this does not appear to be the case. It appears that the collection is aware of the fact it was loaded and then changed, thus it tries to do an update on the "target" datasource instead of an insert.

I am hoping to see if there is a way to have hibernate NOT inject its proxy classes, but at the same time I am wondering if these types of migrations across datasource sessions are a good use case for Hibernate in general.


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