-->
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.  [ 5 posts ] 
Author Message
 Post subject: using multiple SessionFactories to copy list of objects
PostPosted: Fri Apr 16, 2004 5:04 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
hi,

I'm trying to copy some objects from one database to another using two different SessionFactory objects.

From reading the documentation, it seems to me that the following should work:

Configure two different session factories, then 1) get a session from the first; get objects from this session (returns a List); close the session. 2) (without making any changes to anything) get a session from the second factory, iterate through the List, casting the object as required and then saving that object to the 2nd session, and finally committing and closing the second session.

This, however, does not work. I'm using the latest stable version of hibernate and hsqldb (as of a few days ago). I've seen many different exceptions in the course of playing around, and seen many solutions on this forum, but none of them worked for me.

What is the canonical way to read, say, 1000 Person objects from database A and write them to database B?

I'm trying something like the following:

//these create two different SessionFactory objects using
// completely independent configuration files
// each is a hsql database with same tables but at
// different locations on the file system.
private SessionFactory _fromSessions = getFromSessionFactory();
private SessionFactory _toSessions = getToSessionFactory();

Session from = _fromSessions.openSession();
String queryString = "select ordr from test.hibernate.Order as ordr " +
"where ordr.date >= '1999-12-31' " +
"and ordr.date <= '2000-01-02'";
List orders = null;
Transaction tx = null;
try {
tx = from.beginTransaction();
Query q = from.createQuery(queryString);
orders = q.list();
tx.commit();
}
catch (HibernateException he) {
if (tx!=null) tx.rollback();
System.out.println("Couldn't commit from transaction:" + he.getMessage());
} finally {
//from.clear();
from.close();
System.out.println("Closed from.");
}
Session to = _toSessions.openSession();
Transaction tx2 = null;
try {
tx2 = to.beginTransaction();
for (int i = 0; i < ords.length; i++) {
to.saveOrUpdate(ords[i]);
// have also tried save, and various other things
System.out.println("Saved Order to second session: " + ords[i].getId());
}
tx2.commit();
System.out.println("Successfully committed persist to second datasource.");
} catch (HibernateException he) {
if (tx2!=null) tx2.rollback();
System.out.println("Couldn't commit to transaction:" + he.getMessage());
}
finally {
to.close();
System.out.println("Closed to...");
}
Depending on which of several variations on this theme that I try, I get an exception saying that the state couldn't be synchronized with the database, the id of the object is already used by another object, or that the first session is closed (when I'm only trying to commit to the second session).

If anybody could tell me exactly how one can get a collection from one database and put it in another (IDs are assigned in both cases), or point toward a definite and detailed explanation, I'd very much appreciate it.

cheers...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 5:18 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Quote:
id of the object is already used by another object

Use Session.replicate() to preserve ID's.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 5:55 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
[quote="greg_barton"][quote]id of the object is already used by another object[/quote]
Use Session.replicate() to preserve ID's.[/quote]

Thanks for the information. It looks to me though like this is for replicating the object into the same session. Is this correct?

What I want to do is take an object from one session of a sessionfactory, and copy it into a second session from a different sessionfactory (for a different database which has nothing but empty tables at the point that I'm inserting the copied objects).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 6:14 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
No, you can use it for copying between sessions. I do it between two sessions all of the time...about 480 times a day, in fact. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 8:02 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
[quote="greg_barton"]No, you can use it for copying between sessions. I do it between two sessions all of the time...about 480 times a day, in fact. :)[/quote]

Ah, Great!! It works. Thanks so much. You don't know how much time I've spent on this issue with no success, and though I saw at least five other suggestions (which didn't work), I hadn't seen anything at all about replicate.

thanks..


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