0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0x56794E
  • 20,883
  • 13
  • 42
  • 58
  • Maybe [this answer](http://stackoverflow.com/questions/1301399/jpa-using-multiple-database-schemas) is also solution for your problem. – S.Stavreva Apr 07 '16 at 19:42
  • I don't think the answer is that relevant cuz the schema names are known and are independent of the deployment environment. – 0x56794E Apr 08 '16 at 07:56

0 Answers0