theJava
theJava

Reputation: 15034

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

Upvotes: 4

Views: 16218

Answers (3)

JegsVala
JegsVala

Reputation: 1857

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

Upvotes: 6

Jigar Joshi
Jigar Joshi

Reputation: 240860

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.

Upvotes: 8

Arun P Johny
Arun P Johny

Reputation: 388316

SessionFactory contains all the hibernate mapping informations and it is responsible for creation and maintenance of the hibernate session in a transaction.

Upvotes: 3

Related Questions