-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to transfer entities from one database to another
PostPosted: Fri Jul 25, 2008 8:14 am 
Newbie

Joined: Tue Jul 22, 2008 8:56 am
Posts: 3
Hi,

I have a database DB1 with a table T1. The entities stored in T1 have auto-generated IDs:

Code:
@Entity
public class MyEntity
{
  @Id
  @GeneratedValue
  private int id;
}


Now I need to transfer some entities to another database DB2. Both DB1 and DB2 are accessed via Hibernate. A mapping for MyEntity is configured for both databases. If I try the following:

Code:
// session1 is an open session to DB1
// session2 is an open session to DB2

MyEntity entity = (MyEntity) session1.load(...);
session1.evict(entity);
session2.save(entity);



I obviously get an exception because session2 tries to alter the auto-generated ID.

If I remove the @GeneratedValue annotation from the MyEntity class definition it works but then I would have to generate the IDs myself (before saving the entities to DB1) so that would be very ugly and error-prone.

I also tried to write a sublcass to MyEntity (let's say MyEntity2) and to override the id property like the following (i.e. trying to make the id not auto-generated):

Code:
@Entity
public class MyEntity
{
  @Id
  @GeneratedValue
  protected int id;
  ...
}

@Entity
public class MyEntity2 extends MyEntity
{
  @Id
  protected int id;
}


but I get an exception. It seems that you cannot ovverride the annotation

Can anyone suggest a solution?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 2:25 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
The id´s for the entities need to be the same i presume?

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 4:20 pm 
Newbie

Joined: Tue Jul 22, 2008 8:56 am
Posts: 3
jbosseur wrote:
The id´s for the entities need to be the same i presume?


Yes this is exactly the point. As far as I can see there is no way to store entities in both databases with identical IDs.

I actually "solved" the problem by defining a synchronization strategy that works evend if identical entities have different IDs. But it works only in the special context of my application.


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