-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to instantiate more than one SessionFactory?
PostPosted: Thu Jul 13, 2006 2:08 pm 
Newbie

Joined: Wed Jul 05, 2006 10:14 am
Posts: 7
Hi,
in the "Hibernate Reference Documentation" is written in section 3.2:
"Hibernate does allow your application to instantiate more than one SessionFactory. This is useful if you are using more than one database".
Ok, but it´s not written in this documentation how to do it.

So here is my problem: we are writing a system wich is made of several jar files, each jar file correspond to a module of the system.
And many jar files are using Hibernate. But when we try to use more than one jar files, and they are all using differente databases, we get a message wich says that Hibernate could not find entity x, y, z, etc.

How can we instantiate more than one SessionFactory, each one with it´s own configuration file?

Hibernate version: 3.2

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
Henrique Lobo Weissmann


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 7:07 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Yep, set each one up with it's own configuration file. It doesn't appear that the config DTD allows for multipe <session-factory> elements. And if it did, you can't share mapping between the two factories. I have had no problems with multiple config file and it works just fine.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 7:33 am 
Newbie

Joined: Wed Jul 05, 2006 10:14 am
Posts: 7
Ok, but I´m just a beginner: how can I work with multiple configuration files for multiple SessionFactory objects?

_________________
Henrique Lobo Weissmann


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 15, 2006 10:20 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
What I did is create a varient of the HibernateUtil class that is described in the Caveat Emptor demo application (http://caveatemptor.hibernate.org/). Basically, I changed so that it would create n-number of SessionFactories and I can access them by name. So rather than say:

Code:
SessionFactory factory = HibernateUtil.getSessionFactory();


I could do either:

Code:
SessionFactory factory = HibernateUtil.get{name.goes.here}SessionFactory();


Or prehaps a bit more flexible:

Code:
SessionFactory factory = HibernateUtil.getSessionFactory("factoryName);


I also changed the initialization to that it could read a specific URL to load different config files. I could not use the static initialization block that the original HibernateUtil uses to inialize the factory. But about 90% of the configuration code you'll need is already in HibernateUtil, you'll just need to adapt it to you needs. Hope this helps.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 15, 2006 3:22 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Hibernate Reference Documentation
Chapter 3. Configuration
3.7. XML configuration file
last example code


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.