Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of session-factory name property in hibernate-configuration file

In hibernate-cfg.xml file, as per my understanding

hibernate-configuration can have only one session-factory and one or zero security tags

if we configure multiple session-factory then we should get error

"The content of element type "hibernate-configuration" must match "(session-factory,security?)

So anyone tell me

  1. What is the use of the name property in session-factory tag in hibernate.cfg.xml file

    <session-factory name="">
    
  2. In which scenario we can use it?

like image 945
Sasikumar Murugesan Avatar asked Nov 22 '17 13:11

Sasikumar Murugesan


People also ask

What is Session factory name in Hibernate configuration file?

This factory is intended to be shared by all application threads: SessionFactory sessions = cfg. buildSessionFactory(); However, Hibernate does allow your application to instantiate more than one SessionFactory.

What is the use of Session factory?

SessionFactory is an Interface which is present in org. hibernate package and it is used to create Session Object. It is immutable and thread-safe in nature. buildSessionFactory() method gathers the meta-data which is in the cfg Object.

What is Property name in Hibernate?

Hibernate Transaction PropertiesIt represents the classname of a TransactionFactory which is used with Hibernate Transaction API. It represents the classname of a TransactionManagerLookup. It is required when JVM-level caching is enabled.

What is the default name of the Hibernate configuration file?

So, Hibernate has a hard-coded name for the xml configuration file, which it considers as a default name, if no custom argument is provided to the . configure() method, and that name is hibernate. cfg. xml .


2 Answers

Assume that, you must connect to two different databases in your project, so you've two data sources and two session factories. So their names help you to manage them (session factories) easily.

Multiple datasource, Multiple session factories

Sample: inject two session factories from two independent data sources.

@Component
public class TestConfig {

@Autowired
@Qualifier(value="firstSessionFactory")
private SessionFactory sessionFactory;

@Autowired
@Qualifier(value="secondSessionFactory")
private SessionFactory secondDBSessionFactory;

//...

}
like image 65
Mehrdad HosseinNejad Yami Avatar answered Oct 11 '22 12:10

Mehrdad HosseinNejad Yami


You can reference session factory by name,especially if you have multiple configuration files, for example:

change the names inside your cfg.xml file

<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory1"/>
    <!-- ... -->
</bean>

<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory2"/>
like image 31
user7294900 Avatar answered Oct 11 '22 10:10

user7294900