Skip to content

Spring Hibernate

brettwooldridge edited this page Jan 4, 2014 · 3 revisions

The contents of this page are largely lifted from this post.

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.your.package"/>

    <!-- HikariCP configuration -->
    <bean id="mainDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">
        <property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
        <property name="connectionTestQuery" value="SELECT 1" />
        <property name="dataSource.user" value="${jdbc.username}" />
        <property name="dataSource.password" value="${jdbc.password}" />
        <property name="dataSource.url" value="${jdbc.url}" />
    </bean>

    <!-- SPRING - JPA -->
    <jpa:repositories
      base-package="com.your.package.dao" />

    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
      id="transactionManager">
        <property name="entityManagerFactory"
          ref="entityManagerFactory" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
    </bean>

    <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="mainDataSource" />
        <property name="packagesToScan" value="com.your.package.dao.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="showSql" value="false"/>
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
        <property name="jpaProperties">
            <value>
                hibernate.cache.use_second_level_cache = true
                hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory
                hibernate.cache.use_query_cache = true
                hibernate.generate_statistics = true
            </value>
         </property>
    </bean>
</beans>

"The xml can be included in the project web.xml or as an include in another applcationContext."

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext-dao.xml
    </param-value>
</context-param>