views:

983

answers:

3

I'm having a bit of trouble with one particular issue using JPA/Spring:

How can I dynamically assign a schema to an entity?

We have TABLE1 that belongs to schema AD and TABLE2 that is under BD.

@Entity
@Table(name = "TABLE1", schema="S1D")
...

@Entity
@Table(name = "TABLE2", schema="S2D")
...

The schemas may not be hardcoded in an annotation attribute as it depends on the environment (Dev/Acc/Prd). (In acceptance the schemas are S1A and S2A)

How can I achieve this? Is it possible to specify some kind of placeholders like this:

@Entity
@Table(name = "TABLE1", schema="${schema1}")
...

@Entity
@Table(name = "TABLE2", schema="${schema2}")
...

so that schemas are replaced based on a property file residing in the environment?

Cheers

+1  A: 

One thing you can do if you know at deployment is to have 2 orm.xml files. One for schema1 and one for schema2 and then in the persistence.xml you have 2 persistence-units defined. Putting annotations is an anti-pattern if needing to change things like schema

DataNucleus
A: 

Annotation arguments have to be final and can therefore not be changed at runtime.

N

Nico
+1  A: 

You could have two DataSource declarations (one for each schema) in your context.xml and define two persistence units using this datasources. The context.xml can then be different on the appservers of the different environments.

Dominik