H2 Spring Maven Integration

Reading Time: < 1 minute

pom.xml
[code]
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
<scope>compile</scope>
</dependency>
[/code]

database.properties
[code]
orm.connection.driver_class=org.h2.Driver
orm.connection.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
orm.connection.username=sa
orm.connection.password=
orm.dialect=org.hibernate.dialect.H2Dialect
orm.pool_size = 1
orm.show_sql=true
orm.hbm2ddl.auto=create/update
[/code]

spring.xml
[code]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/css/**" location="/css/" />
<context:component-scan base-package="com.xxx" />
<context:annotation-config />

<!– Property loader for database –>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:database.properties" />

<!– Hibernate connection configuration –>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${orm.connection.driver_class}" />
<property name="url" value="${orm.connection.url}" />
<property name="username" value="${orm.connection.username}" />
<property name="password" value="${orm.connection.password}" />
</bean>

<!– Hibernate configuration settings –>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.xxx.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${orm.dialect}</prop>
<prop key="hibernate.pool_size">"${orm.pool_size}</prop>
<prop key="hibernate.show_sql">${orm.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${orm.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<!– Hibernate Transaction –>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!– H2 Web Console –>
<bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer"
init-method="start" destroy-method="stop" depends-on="h2WebServer">
<constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,21092" />
</bean>
<bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer"
init-method="start" destroy-method="stop">
<constructor-arg value="-web,-webAllowOthers,-webPort,21082" />
</bean>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<!– Localization Configuration –>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/i18n/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>

<bean id="cookieResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
<property name="cookieName" value="my-locale-cookie" />
<property name="cookieMaxAge" value="3600" />
</bean>

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>

</beans>
[/code]