Andreas L.
Andreas L.

Reputation: 2923

Can I set the schema before creating the EntityManager?

I need to use multiply persistence units with different properties (in my case MySQL and Oracle database). In persistence.xml I define two different "persistence-unit"s and list only the entity classes there.

Properties could be set in persitence.xml with

<properties> <property name="..." value="..." /> ...

Im doing it in a java class before creating the EntityManager, because I must use different properties (which I read before):

        EntityManagerFactory factory;
        ...
        HashMap<String, String> dbProperties = new HashMap<String, String>();
        dbProperties.put("javax.persistence.jdbc.driver", driver);
        dbProperties.put("javax.persistence.jdbc.url", url);
        dbProperties.put("javax.persistence.jdbc.user", user);
        dbProperties.put("javax.persistence.jdbc.password", password);
        dbProperties.put("eclipselink.ddl-generation", "none");
        dbProperties.put("eclipselink.ddl-generation.output-mode", "database");

        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, dbProperties);
        EntityManager em = factory.createEntityManager();
        ...

For Oracle I need to set schema dynamicly (if possible) and not hardcoded in @Table annotation in each Entity class.

For now I am using a constant (means it is not dynamicly)

@Table(name="MYTABLE", schema = Constants.ORACLE_SCHEMA)

I want to use

@Table(name="MYTABLE")

And set the schema as property

dbProperties.put(...)

Is there such a property?

On my search I found a syntax which could help

ALTER SESSION SET CURRENT_SCHEMA=MYSCHEMA

But I don't know how to combine it with EntityManager.

I have allready asked about all available properties here, but could not find anything yet.

Thank you in advance.

Upvotes: 2

Views: 9153

Answers (2)

rjdkolb
rjdkolb

Reputation: 11838

I am not sure if this will solve your problem, but several application server vendors may something called InitSql.

It is a SQL statement that is run on every successful database connection made.

  • In Weblogic 12c this is called InitSql
  • In Tomcat 7 this is also called InitSql

I can't find something like this for EclipseLink in Java SE, but perhaps there is a post checkout SQL that can be run like InitSql

Like Andreas L.'s answer this can allow you to run commands like .

ALTER SESSION SET CURRENT_SCHEMA=xxx

Upvotes: 1

Andreas L.
Andreas L.

Reputation: 2923

I have found a solution for this with the help of this post: https://community.oracle.com/thread/2270534?tstart=0

It can be done with a query after EntityManager is created:

        ...
        EntityManager em = factory.createEntityManager();
        Query query;

        em.getTransaction().begin();
        query = em.createNativeQuery("ALTER SESSION SET CURRENT_SCHEMA="+schemaName);
        query.executeUpdate();
        em.getTransaction().commit();

Check current schema with:

        query = em.createNativeQuery("SELECT SYS_CONTEXT('USERENV','SESSION_SCHEMA') FROM DUAL");
        System.out.println("Current schema: " + query.getResultList());

Upvotes: 2

Related Questions