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
What is the use of the name property in session-factory tag in hibernate.cfg.xml file
<session-factory name="">
In which scenario we can use it?
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.
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.
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.
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 .
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;
//...
}
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With