-->
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.  [ 26 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: changing hibernate shemas at runtime
PostPosted: Thu Oct 06, 2005 10:49 am 
Beginner
Beginner

Joined: Tue Aug 17, 2004 11:27 pm
Posts: 24
In our environment, we have three logical schemas in one database. Since all of these schemas are accessed from the same session factory and data source, we need to include the schema attribute in our hibernate mapping files. We would like to be able to create new sandbox environments by just adding new schemas in the same database. The problem, however, is that the schema names are already hard coded in the hibernate mapping files. Is it possible to change the values of the schema attributes at runtime?

Thanks.

-karl



Hibernate version: 3.05

Name and version of the database you are using: UDB 8.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 11:23 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
You can do it with
configuration.setProperty( "hibernate.default_schema",value)

see API for Configuration class


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 11:35 am 
Beginner
Beginner

Joined: Tue Aug 17, 2004 11:27 pm
Posts: 24
I don't think hibernate.default_schema would help me in this case since we have three different schemas for one session factory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 1:28 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
ste schema in *hbm.xml file or create 3 sessionFactory's for 3 schemas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 1:51 pm 
Beginner
Beginner

Joined: Tue Aug 17, 2004 11:27 pm
Posts: 24
Quote:
set schema in *hbm.xml file


We already set the schema in the hbm.xml file. The issue is we want to override this setting at runtime in places like dev and qa. The hibernate mappings are contained within the actual jars. We want to be able to move from dev to qa to prod without rebuilding these jars.

Quote:
create 3 sessionFactory's for 3 schemas


Three separate session factories would work, but I think it might be somewhat inefficient. We really only need one session factory since there is only one database.

Thanks.

-karl


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:03 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
How do you resolve in another application ?

you can change schema in class definition, define session factory ant what yet

if you have one program (one JVM) and three schemas create sessionfactory, but If you want production and db set property schema in your program (no in *.cfg.xml) then call

Code:
Configuration c = new Configration();
c.setProperty("hibernate.default_schema",yourSchemaProperty);
c.configure();


Top
 Profile  
 
 Post subject: Re: changing hibernate shemas at runtime
PostPosted: Thu Oct 06, 2005 2:10 pm 
Newbie

Joined: Wed Sep 14, 2005 12:17 pm
Posts: 13
kbaum wrote:
In our environment, we have three logical schemas in one database. [...] Is it possible to change the values of the schema attributes at runtime?


Here's a suggestion that might be a bit of extra work on your part.

When you configure your Hibernate session, rather than list all of your *.hbm.xml files in your Hibernate config file, you could load them manually using Configuration.addDocument(Document hbm);

Just before you add the hbm file, you could manipulate the table="schema1.MY_TABLE" attribute of each class element to, say, turn it into, say, "sandbox_schema1.MY_TABLE".

Since this would only happen at one time (Hibernate config time), it wouldn't really affect the runtime performance, but it'd probably be a slightly annoying amount of code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:14 pm 
Beginner
Beginner

Joined: Tue Aug 17, 2004 11:27 pm
Posts: 24
Quote:
Just before you add the hbm file, you could manipulate the table="schema1.MY_TABLE" attribute of each class element to, say, turn it into, say, "sandbox_schema1.MY_TABLE".


I think this is an excellent idea.

Thanks!

-karl


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:18 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
kbaum wrote:
Quote:
Just before you add the hbm file, you could manipulate the table="schema1.MY_TABLE" attribute of each class element to, say, turn it into, say, "sandbox_schema1.MY_TABLE".


I think this is an excellent idea.

Thanks!

-karl


it is easier manipulate with every *hbm.xml than with one *cfg.xml and Configuration
Congratulations.Go ahaed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:24 pm 
Beginner
Beginner

Joined: Tue Aug 17, 2004 11:27 pm
Posts: 24
Quote:
it is easier manipulate with every *hbm.xml than with one *cfg.xml and Configuration
Congratulations.Go ahaed


I cannot set this in the *cfg.xml since I have only one session factory for three schemas. I understand i could have a session factory per schema, but at this point it's too big of a change for our project.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:32 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
You set *cfg.xml without default schema and in HibernateUtil (or what ever for SessionFactory) next :
Code:
Configuration c = new Configration();
c.setProperty("hibernate.default_schema",yourSchemaProperty);
sessionFactory = c.configure();


your SchemaProperty is one from your schemas.You can make 3 *cfg.xml with your 3 schemas and call with this :
Code:
Configuration c = new Configration();
sessionFactory = c.configure(yourCfgXmlName);


You can change any parameter in Configuration see chapter 4.1 in documentation (Programmatic configuration)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:35 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I think you are missing the point of snpesnpe's post. Don't put any schema in the hbms. Then you can set the default_schema at runtime through the Configuration and point to any schema you want.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:43 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Setting schema in mapping file (hbm) is bad idea general .Don't do it, if isn't necessary


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:51 pm 
Newbie

Joined: Wed Sep 14, 2005 12:17 pm
Posts: 13
nathanmoon wrote:
I think you are missing the point of snpesnpe's post. Don't put any schema in the hbms. Then you can set the default_schema at runtime through the Configuration and point to any schema you want.


I think you're missing the point of kbaum's post: there are three schemas in place in the production environment, and another three schemas in the sandbox environment.

Either kbaum needs three session factories per application instance (and that might have impacts on joins) or zie has to specify which hbms have which schemas.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:58 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Quote:
In our environment, we have three logical schemas in one database. Since all of these schemas are accessed from the same session factory and data source, we need to include the schema attribute in our hibernate mapping files. We would like to be able to create new sandbox environments by just adding new schemas in the same database. The problem, however, is that the schema names are already hard coded in the hibernate mapping files. Is it possible to change the values of the schema attributes at runtime?

Thanks.


yes, kbaum can change every *hbm.xml on runtime or remove schema from *hbm.xml once and change schema in configuration - hardcoded schema name will make problem always


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

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.