Bootstrapping Hibernate 3 with Spring | Code Factory

Code Factory
3 min readApr 10, 2020

--

Reference Link : Link

Donate : Link

1. Overview

This article will focus on setting up Hibernate 3 with Spring — we’ll look at how to use both XML and Java configuration to set up Spring with Hibernate 3 and MySQL.

2. Java Spring Configuration for Hibernate 3

Setting up Hibernate 3 with Spring and Java config is straightforward :

package com.codeFactory.configuration;import java.util.Properties;import javax.sql.DataSource;import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan({ "org.codeFactory" })
public class DBConfiguration {
@Bean
public AnnotationSessionFactoryBean sessionFactory() {
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.codeFactory" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test_connection");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", "update");
setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
}
};
}
}

Compared to the XML Configuration — described next — there is a small difference in the way one bean in the configuration access another. In XML there is no difference between pointing to a bean or pointing to a bean factory capable of creating that bean. Since the Java configuration is type-safe — pointing directly to the bean factory is no longer an option — we need to retrieve the bean from the bean factory manually :

txManager.setSessionFactory(sessionFactory().getObject());

3. XML Spring Configuration for Hibernate 3

Similarly, we can set up Hibernate 3 with XML config as well :

hibernate3configuration.xml (create file in src/main/resources)

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.codeFactory"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">
update
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test_connection"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

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

<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

Then, this XML file is bootstrapped into the Spring context using a @Configuration class :

package com.codeFactory.configuration;import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ImportResource({ "classpath:hibernate3configuration.xml" })
public class DBConfiguration {
}

4. Spring, Hibernate and MySQL

The example above uses MySQL 5 as the underlying database configured with Hibernate — however, Hibernate supports several underlying SQL Databases.

4.1. The Driver

The Driver class name is configured via the jdbc.driverClassName property provided to the DataSource.

In the example above, it is set to com.mysql.jdbc.Driver from the mysql-connector-java dependency we defined in the pom, at the start of the article.

4.2. The Dialect

The Dialect is configured via the hibernate.dialect property provided to the Hibernate SessionFactory.

In the example above, this is set to org.hibernate.dialect.MySQL5Dialect as we are using MySQL 5 as the underlying Database. There are several other dialects supporting MySQL:

  • org.hibernate.dialect.MySQL5InnoDBDialect — for MySQL 5.x with the InnoDB storage engine
  • org.hibernate.dialect.MySQLDialect — for MySQL prior to 5.x
  • org.hibernate.dialect.MySQLInnoDBDialect — for MySQL prior to 5.x with the InnoDB storage engine
  • org.hibernate.dialect.MySQLMyISAMDialect — for all MySQL versions with the ISAM storage engine

Hibernate supports SQL Dialects for every supported Database.

5. Usage

At this point, Hibernate 3 is fully configured with Spring and we can inject the raw Hibernate SessionFactory directly whenever we need to :

package com.codeFactory.controller;import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Autowired
SessionFactory sessionFactory;
}

6. Maven

Continuing to Hibernate 3, the Maven dependencies are simple :

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>

The spring-orm module provides the Spring integration with Hibernate :

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.18.RELEASE</version>
</dependency>

Then, to enable Hibernate to use its proxy model, we need javassist as well :

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>

We’re going to use MySQL as our DB for this tutorial, so we’ll also need :

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
<scope>runtime</scope>
</dependency>

And finally, we will not be using the Spring data source implementation — the DriverManagerDataSource; instead, we’ll use a production-ready connection pool solution – Tomcat JDBC Connection Pool :

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>7.0.55</version>
</dependency>

--

--