[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Reconfigure the

ThorstenMaier do-not-reply at jboss.com
Wed Jul 30 02:54:31 EDT 2008


I want a seperate DB schema for every mandator in our application so that the data is not mixed between different mandators. The schemas should be generate dynamicly during runtime. I use something like this to generate the tables of the schema:

  | Map<String, String> props = new Hashtable<String, String>();
  | props.put(HIBERNATE_DEFAULT_SCHEMA, userName);
  | Ejb3Configuration cfg = new Ejb3Configuration();
  | Ejb3Configuration configured = configured = cfg.configure(persistenceUnitName, properties);
  | String[] lines = cfg.getHibernateConfiguration().generateSchemaCreationScript(new PostgreSQLDialect());
  | for (int i = 0; i < lines.length; i++) {
  |   entityManager.createNativeQuery(lines).executeUpdate();
  | }

So far every thing work pretty good. The tables in the database are generated during runtime for a new "userName" (I generate the schema in the db with something like "CREATE SCHEMA %%username%%" before)

So far so good. Now my problem:
I want to persist some data into the new generated tables within the new schema. For that I want to change the property "hibernate.default_schema" of the persistence.xml config file during runtime. 

I tried several things to do so, but nothing help me to solve the problem. I'm not able to generate a new EntityManager because of transaction reasons. So I have to change the existing EntityManager during runtime.

Different things I tried:

1. EntityManagerFactory
I use the EntityManagerFactory and call the method "createEntityManager(Map arg)". My map looks like this:

  | Map<String, String> props = new Hashtable<String, String>();
  | props.put("hibernate.default_schema", userName);

I get a new EntityManager but this instance is still using the hibernate.default_schema out of persistence.xml

2. Ejb3Configuration
I use the Ejb3Configuration class to generate a new EntityManager with the new "hibernate.default_schema". Perhaps this would work, but I can't do this because of transaction reasons.

2. Hibernate Delegate

  | Map<String, String> props = new Hashtable<String, String>();
  | props.put("hibernate.default_schema", userName);
  | SessionImpl sessionImpl = ((org.hibernate.impl.SessionImpl) entityManager.getDelegate());
  | sessionImpl.getFactory().getConnectionProvider().configure(props);

I tried to use the Hibernate delegate of JPA to reconfigure the ConnectionProvider too. After that I establish a new database connection, but the "default_schema" do not change after this reconfiguration.

Is it possible to reconfigure the "hibernate.default_schema" during runtime? And how?

Hibernate version: 3.2.4.sp1 

App-Server: JBoss 4.2.3.GA 

DB: postgresql-8.3-603

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4167506#4167506

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4167506

More information about the jboss-user mailing list