0

I have two different databases D1, D2 with a same Table T.

T has a primary Key column id, that is autogenerated in D1, a simple integer in D2. I've to shift record from T in D1 to T in D2, so I mapped T as an @Entity with JPA. The problem is that @ID is @GeneratedValue(strategy = GenerationType.IDENTITY) for D1 but not for D2.

Is there a solution for not duplicating the Entity? I noticed that using a @MappedSuperclass i need an @Id column but in my case is what I'm trying to specialize. The only solution I found is to generate two different classes referencing the same Table that are identical apart from the @id... anything better?

(if I use a common abstract class I've problems when defining @NamedQueries 'cause it seems that inherited field cannot be referenced (SELECT m FROM Specialized m WHERE m.aBaseField= :aBaseField) return an exception : "The state field path 'm.aBaseField' cannot be resolved to a valid type."

Francesco Umani
  • 69
  • 4
  • 10

2 Answers2

1

I would define two different persistence units, one for each database. Then you can customize one with an orm.xml file.

James
  • 17,965
  • 11
  • 91
  • 146
  • Thanks James, I use annotation but I think it's the same. I just have two persistence units, so duplicating the entity class or orm.xml, is a solution, but a dirty one :). – Francesco Umani Aug 07 '13 at 14:46
0

I didn't find a real solution. What I ended doing was to duplicate the class providing different @Entity(name="name1") and removing for one of them the @GenerationType annotation. In the persistence unit I use the one or the other accordingly to what I'm going to do.

Francesco Umani
  • 69
  • 4
  • 10