12

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?

1
  • do u have multiple databases to be connected with same application? Commented Jan 9, 2020 at 14:28

3 Answers 3

7
+100

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;

//...

}
Sign up to request clarification or add additional context in comments.

Comments

1

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"/>

Comments

1

when we have to connect multiple database then we have to create Multiple datasource for that we have create Multiple session factories

@Component
public class Config {

@Autowired
@Qualifier(value="FirstSessionFactory")
private SessionFactory firstSessionDB;

@Autowired
@Qualifier(value="SecondSessionFactory")
private SessionFactory secondSessionDB;
// for the third database
@Autowired
@Qualifier(value="thirdSessionFactory")
private SessionFactory thirdSessionDB;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.