Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two SessionFactory in single hibernate.cfg.xml

 
Barry Van
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ecpfriends ecpfriends" please check your private messages.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Else, can i use more than one Hibernate.cfg.xml file? If yes. please give me example..
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul. But I need one example code(cfg.xml's)please.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation contains an example. Its also worth searching through this forum; there will be hundreds of examples.
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic