Question

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."

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top