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?