-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Change of "hibernate.default_schema" during runtim
PostPosted: Tue Jul 29, 2008 11:04 am 
Newbie

Joined: Tue Jul 29, 2008 10:45 am
Posts: 1
Hello,

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:

Code:
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[i]).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:

Code:
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

Code:
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 7:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I don't have the answer to your particular question, but have you seen this:http://www.hibernate.org/429.html. Maybe the best way forward is implementing your own data source or using Hibernate Shards. Be aware of the cons though.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.