File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes If my hibernate-cfg.xml has multiple session factories then how can I Load appropriate SessionFactor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "If my hibernate-cfg.xml has multiple session factories then how can I Load appropriate SessionFactor" Watch "If my hibernate-cfg.xml has multiple session factories then how can I Load appropriate SessionFactor" New topic
Author

If my hibernate-cfg.xml has multiple session factories then how can I Load appropriate SessionFactor

Rajendra Lella
Greenhorn

Joined: Oct 29, 2009
Posts: 9
Hi,

Can any one please tell if my hibernate-cfg.xml has multipe SessionFactories like
<hibernate-configuration>
<session-factory>
-- Connection to oracle
</session-factory>
<session-factory>
-- Connection to MySql
</session-factory>
<session-factory>
-- Connection to Sybase DB
</session-factory>
<session-factory>
-- Connection to Other DB
</session-factory>
Here How can I load the appropriate DB in my Code
Like I want to open Connection to Oracle in My code
Configuration con = new Configuration().configure();
SessionFactory sf = con.buildSessionFactory();// Here how can I load the appropriate DB Like (oracle,Sybase .. etc)

Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Rajendra Lella wrote:Hi,

Can any one please tell if my hibernate-cfg.xml has multipe SessionFactories like
<hibernate-configuration>
<session-factory name=>
-- Connection to oracle
</session-factory>
<session-factory >
-- Connection to MySql
</session-factory>
<session-factory>
-- Connection to Sybase DB
</session-factory>
<session-factory>
-- Connection to Other DB
</session-factory>
Here How can I load the appropriate DB in my Code
Like I want to open Connection to Oracle in My code
Configuration con = new Configuration().configure();
SessionFactory sf = con.buildSessionFactory();// Here how can I load the appropriate DB Like (oracle,Sybase .. etc)



One way is to use different oracle.cfg.xml mysql.cfg.xml configuration file.
And then
The same can be acheived in on cfg fille also using the sessionFactory name tag..

hope it helps you i am also learning Hibernate.
Rajendra Lella
Greenhorn

Joined: Oct 29, 2009
Posts: 9
Vivek,
apart from that is there any other way of invoking SessionFactory of another DB?

regards,
Rajendra
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can't define a configuration file that contains more than one session factory; a hibernate-configuration element can only contain one session-factory according to the dtd. You can define more than one configuration file, as VivekSingh points out. Alternatively you can programatically build your sessions factories (see the Configuration API).

Normally you would not define your connection in your session factory, you would define it as a DataSource and refer to the DataSource from your configuration.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Paul Sturrock wrote:You can't define a configuration file that contains more than one session factory;


Yes its true i think, i have checked lot of places but its not possible to define all sessionFactory in one cfg.xml file.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: If my hibernate-cfg.xml has multiple session factories then how can I Load appropriate SessionFactor
 
Similar Threads
hbm file version working but annotation not
Really strange error: Could not initialize class net.sf.hibernate.impl.SessionFactoryImpl
Persistence: Calling Stored Procedures from Hibernate
More than SessionFactory in single hibernate.cfg.xml?
Connecting to Multiple databases in hibernate