Spring 4 + Hibernate 4 + Gradle Integration Example using Annotation
July 28, 2014
On this page, we will learn Spring 4 and Hibernate 4 integration using annotation and the project will be build using Gradle. For Hibernate 4, spring provides LocalSessionFactoryBuilder, to get SessionFactory. In our example we are using MySQL to save data. We will not use hibernate.cfg.xml. We are using BasicDataSource to get DataSource. Learn step by step the integration of spring 4 and hibernate 4 now.
Software Used
In our example we are using below software to build and run Spring 4 and Hibernate 4 integration.1. JDK 7
2. Eclipse
3. Gradle 2.0
4. MySQL 5.5
Project Structure in Eclipse
Find the below print screen that is showing how the project structure is there in eclipse.Gradle Script for JAR Dependency
We are using Gradle to build the project and eclipse classpath for JAR dependency.build.gradle
apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'Concretepage' version = '1.0-SNAPSHOT' repositories { mavenCentral() } jar { manifest { attributes 'Main-Class': 'com.concretepage.Spring4Hibernate4Test' } } dependencies { compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.1.4.RELEASE' compile 'org.hibernate:hibernate-core:4.3.6.Final' compile 'javax.servlet:javax.servlet-api:3.1.0' compile 'org.slf4j:slf4j-simple:1.7.7' compile 'org.javassist:javassist:3.15.0-GA' compile 'mysql:mysql-connector-java:5.1.31' compile 'commons-dbcp:commons-dbcp:1.4' }
Create Database Table
For the demo we have created a table in MySQL database. Find the script to create the table.Table: person
CREATE TABLE `person` ( `id` INT(10) NULL DEFAULT NULL, `name` VARCHAR(50) NULL DEFAULT NULL ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB;
Java Entity to Persist Data
Find the entity that is representing the person table. There is two column id and name.Person.java
package com.concretepage.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="person") public class Person { @Id @Column(name="id") private int id; @Column(name="name") private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Configuration Class: Create SessionFactory Using LocalSessionFactoryBuilder
This is the most important class which will play the role of integrating Spring 4 and Hibernate 4. We are using BasicDataSource to get DataSource. This DataSource is passed to LocalSessionFactoryBuilder class to get SessionFactory. Finally SessionFactory is passed to HibernateTemplate. For transaction management we are using HibernateTransactionManager .AppConfig.java
package com.concretepage.config; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.hibernate.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.orm.hibernate4.HibernateTemplate; import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.concretepage.dao.IPersonDao; import com.concretepage.dao.PersonDao; import com.concretepage.entity.Person; @Configuration @EnableTransactionManagement public class AppConfig { @Bean public IPersonDao personDao() { return new PersonDao(); } @Bean public HibernateTemplate hibernateTemplate() { return new HibernateTemplate(sessionFactory()); } @Bean public SessionFactory sessionFactory() { return new LocalSessionFactoryBuilder(getDataSource()) .addAnnotatedClasses(Person.class) .buildSessionFactory(); } @Bean public DataSource getDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/concretepage"); dataSource.setUsername("root"); dataSource.setPassword(""); return dataSource; } @Bean public HibernateTransactionManager hibTransMan(){ return new HibernateTransactionManager(sessionFactory()); } }
Dao Classes
Create the Dao class to save person in the database. Make it @Transactional to recover database transaction failure. Find the interface.IPersonDao.java
package com.concretepage.dao; public interface IPersonDao { public void savePerson(); }
PersonDao.java
package com.concretepage.dao; import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate4.HibernateTemplate; import com.concretepage.entity.Person; @Transactional public class PersonDao implements IPersonDao { @Autowired private HibernateTemplate hibernateTemplate; public void savePerson() { Person person = new Person(); person.setId(1); person.setName("Ram"); hibernateTemplate.save(person); } }
Main Class
Finally test the Spring 4 and Hibernate 4 Integration.Spring4Hibernate4Test.java
package com.concretepage; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.concretepage.config.AppConfig; import com.concretepage.dao.IPersonDao; public class Spring4Hibernate4Test { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); IPersonDao pdao = ctx.getBean(IPersonDao.class); pdao.savePerson(); System.out.println("Done"); } }
Run the main class and check the database for person that is saved.