Open In App

How to Create a Project using Spring MVC and Hibernate 5?

Spring MVC is a popular model view controller framework that handles dependency injection at run time. Hibernate 5 is an ORM framework that acts as an abstraction over the database. It allows interaction with the underlying database by removing any implementation details which is handled by hibernate. In this article, we will integrate Spring MVC and Hibernate 5 with an in-memory database h2.

Requirements 

Required Dependencies

After creating an empty project you will need some core dependencies which are required for a bootstrap setup. In the dependencies section of your pom.xml add the following dependencies



spring-orm

The spring-orm module provides integration with hibernate






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

hibernate-core

Core hibernate module for the hibernate dependencies




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

h2-database 

In-memory database




<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.197</version>
</dependency>

commons-dbcp2

commons dependency for connection pooling




<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>2.7.0</version>
</dependency>

lombok

helps generate boilerplate code for constructors/setters/getters etc




<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>

Configuration

Hibernate requires certain configurations to bootstrap such as DB path, driver, username, password, etc. We are going to use the annotation-based configuration an XML-based configuration can also be used if needed. 

Create a db.properties file:

db.properties file will contain the basic configurations of the database we will read this source to configure our hibernate session

# MySQL properties
db.driver=org.h2.Driver
db.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
db.username=sa
db.password=sa

# Hibernate properties
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.H2Dialect

Configuring the Application

Create a configuration class to wire the details for hibernate and set up the session factory etc




import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
  
import javax.sql.DataSource;
import java.util.Properties;
  
@EnableTransactionManagement
@Configuration
@PropertySource("classpath:db.properties")
public class ExampleConfiguration {
    
    @Autowired
    private Environment env;
  
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(
                new String[]{"com.example"});
        sessionFactory.setHibernateProperties(hibernateProperties());
  
        return sessionFactory;
    }
  
    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.url"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }
  
    @Bean
    public PlatformTransactionManager hibernateTransactionManager() {
        HibernateTransactionManager transactionManager
                = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }
  
    private final Properties hibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty(
                "hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        hibernateProperties.setProperty(
                "hibernate.dialect", env.getProperty("hibernate.dialect"));
        return hibernateProperties;
    }
}

Model class

model class for the entity/table




package example.model;
  
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
  
import javax.persistence.*;
  
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "user")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    
    @Column(name = "first_name")
    private String firstName;
    
    @Column(name = "last_name")
    private String lastName;
}

Repository class

The persistence layer to persist the model in the database




package example.repository;
  
import example.model.User;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
  
@Repository
public class UserRepository {
    
    @Autowired
    private SessionFactory sessionFactory;
  
    public void addUser(User user) {
        sessionFactory.getCurrentSession().save(user);
    }
  
    public User getUsers(int id) {
        return sessionFactory.getCurrentSession().get(User.class, id);
    }
}

Service class

service class to interact with the repository and persist items




package example.service;
  
import example.model.User;
import example.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
  
import javax.transaction.Transactional;
  
@Service
@RequiredArgsConstructor
public class UserService {
    
    @Autowired
    private final UserRepository userRepository;
  
    @Transactional
    public void addUser(User user) {
        userRepository.addUser(user);
    }
  
    @Transactional
    public User getUser(int id) {
        return userRepository.getUsers(id);
    }
}

Application class

This is the entry point of the application, we need to wire this configuration to the application context.




package example;
  
import example.model.User;
import example.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
public class ExampleApplication {
    public static void main(String[] args) {
        
        System.out.println("Starting application");
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ExampleConfiguration.class);
        UserService userService = context.getBean(UserService.class);
          
        // add user to the repo
        userService.addUser(new User(1, "john", "doe"));
          
        // search for the user in the repo
        User user = userService.getUser(1);
        System.out.println(user.toString());
        
    }
}

Output:

Hibernate: drop table user if exists
Hibernate: create table user (id integer generated by default as identity, first_name varchar(255), last_name varchar(255), primary key (id))
Oct 17, 2022 3:44:14 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: insert into user (id, first_name, last_name) values (null, ?, ?)
Hibernate: select user0_.id as id1_0_0_, user0_.first_name as first_na2_0_0_, user0_.last_name as last_nam3_0_0_ from user user0_ where user0_.id=?
User(id=1, firstName=john, lastName=doe)

Package Structure:

 


Article Tags :