Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 950
theJava Avatar asked Dec 04 '22 10:12

theJava


1 Answers

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.

like image 85
jmj Avatar answered Dec 19 '22 08:12

jmj