I'm working on a Glassfish Java EE application with Oracle database.
The problem is that historically, the application is only meant to be deployed on one single environment on which the database has two users: UserA
and UserB
.
UserA
"owns" all of the tables that the application needs. Let's call this the "admin user".
UserB
doesn't have any table and only has read access on the above-mentioned tables.
However, the application's persistence unit accesses the database via a UserB
.
So what people did was they specify the schema on all of the entity classes like this: (The string "UserA" is hard-coded)
@Entity
@Table(name="FooBar", schema="UserA")
public class FooBar
{ }
This all works well until the application is deployed in another environment where the "admin user" and the "readonly" user are NOT named UserA
and UserB
anymore.
The app breaks since it couldn't find the tables it needs. How can I get this to work CLEANLY?
Here's my hack around this, but I don't think it's a good idea.
I specify a JVM option in the glassfish domains.xml file like -Denv=dev
or -Denv=prod
, etc.
However, since these are evaluated at runtime and the string specified in schema is required to be a constant, I have some thing like this:
public class ConfigLoader
{
public static final String SCHEMA_NAME;
//static block to set the name
static
{
//code to pull on the jvm option goes here
}
}
Then I use that SCHEMA_NAME
string to specify the schema on entity classes.