Pages

Wednesday, November 17, 2010

Hibernate - Spring MVC Integration

Here are the steps to configure Hibernate Step By Step in your Spring MVC:

Step 1: In your spring-dao-context.xml (can be any name which holds all the database related stuff) file, configure the Hiberante using below configuration:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceEX"/>
        <property name="configLocation" value="classpath:/hibernate/hibernate.cfg.xml"/>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.provider_class">com.opensymphony.oscache.hibernate.OSCacheProvider</prop>
                <prop key="com.opensymphony.oscache.configurationResourceName">/oscache.properties</prop>

                <!-- setting 'show_sql' to true ends up logging sql statements to console.
                     Instead, we want to log into the log file configured via log4j.
                     So, use the "log4j.logger.org.hibernate.SQL=DEBUG" setting in the
                     log4j.properties file instead -->
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
        <property name="entityInterceptor">
            <bean class="com.sritech.example.entities.AuditInterceptor"/>
        </property>
        <property name="useTransactionAwareDataSource" value="false"/>
    </bean>

-> dataSourceEX: - This is defined in spring-context.xml file as below:

 <bean id="dataSourceEX" class="com.sritech.example.OracleDataSourceWrapper">
        <property name="targetDataSource" ref="targetDataSourceEX" />
    </bean>

<jee:jndi-lookup id="targetDataSourceEX" jndi-name="ExampleJndi"/>

-> hibernate.cfg.xml:- This file should be created any where under resources which should reside in class path.

Example file looks like below:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- All the query xml files should be defined in this file.-->

<hibernate-configuration>
    <session-factory>
        <mapping resource="hibernate/queries/lookups-queries.hbm.xml" /> 
    </session-factory>
</hibernate-configuration>

No comments:

Post a Comment