This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Two SessionFactory in single hibernate.cfg.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Two SessionFactory in single hibernate.cfg.xml" Watch "Two SessionFactory in single hibernate.cfg.xml" New topic
Author

Two SessionFactory in single hibernate.cfg.xml

Barry Van
Greenhorn

Joined: Oct 23, 2008
Posts: 22
Can we have more than one session-factory in single hibernate.cfg.cml file as below code?
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory name="monitor">
.
.
</session-factory>

<session-factory name="horse">
.
.
</session-factory>
</hibernate-configuration>
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"ecpfriends ecpfriends" please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by ecpfriends ecpfriends:
Can we have more than one session-factory in single hibernate.cfg.cml file as below code?
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory name="monitor">
.
.
</session-factory>

<session-factory name="horse">
.
.
</session-factory>
</hibernate-configuration>


No - the DTD doesn't allow it. You can use more than one SessionFactory in an application however.
Barry Van
Greenhorn

Joined: Oct 23, 2008
Posts: 22
Else, can i use more than one Hibernate.cfg.xml file? If yes. please give me example..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes you can. Of course they can't both be called hibernate.cfg.xml and you can't rely on the default configuration mechanism. Have a look at the JavaDocs for the Configuration class, you will see how you configure it.
Barry Van
Greenhorn

Joined: Oct 23, 2008
Posts: 22
Thank you Paul. But I need one example code(cfg.xml's)please.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The documentation contains an example. Its also worth searching through this forum; there will be hundreds of examples.
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Barry, you can use JPA with hibernate that support the definition of multiple persistence context (could be the same as a SessionFactory in hibernate) at once, the price to pay it's a little overhead in the application size because of the additional jars that you have you add to your classpath,


regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two SessionFactory in single hibernate.cfg.xml