Hibernate SessionFactory
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
What is SessionFactory class? Why do we use it? What is hibernateTemplate Class used for?
<bean id="myUserDAO" class="com.mysticcoders.mysticpaste.services.ContactSerImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.mysticcoders.mysticpaste.model.Contact</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"&g开发者_JAVA技巧t;org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
What does this do in bean
Application obtains session instances from Session Factory.
SessionFactory
is mostly configured as Singleton
in application ,
If you are using Spring it will be configured in application context to be made singleton.
SessionFactory
caches generate SQL statements and other
mapping metadata that Hibernate uses at runtime.
Cached data that has been read in one unit of work and may be reused in a future unit of work.
You can obtain object of session factory from Configuration class
SessionFactory sessionFactory =
Configuration.buildSessionFactory();
Here in your conf. you have configured sessionFactory using AnnotationSessionFactoryBean class
bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
and you have set some properties of session factory those are needed.
HibernateTemplate
is a class provided by Spring :
Helper class that simplifies Hibernate data access code. Automatically converts HibernateExceptions into DataAccessExceptions, following the org.springframework.dao exception hierarchy.
SessionFactory is as Interface it delivery the of session object for whole application or entire hibernate application.
There will be generally one SessionFactory and can be shared by all application thread. SessionFactory is thread-safe.
SessionFactory is second level cache of data that is reusable between transaction at a process or cluster level.
Continue.......
SessionFactory contains all the hibernate mapping informations and it is responsible for creation and maintenance of the hibernate session in a transaction.
精彩评论