开发者

Hibernate SessionFactory

开发者 https://www.devze.com 2023-02-01 23:31 出处:网络
private HibernateTemplate hibernateTemplate; public void setSessionFactory(SessionFactory 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.


  1. SessionFactory is as Interface it delivery the of session object for whole application or entire hibernate application.

  2. There will be generally one SessionFactory and can be shared by all application thread. SessionFactory is thread-safe.

  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号