How to Create a Project using Spring MVC and Hibernate 5?
Last Updated :
28 Apr, 2025
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
- Maven
- IDE (Preferably IntelliJ)
- Java 8+
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
XML
<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
XML
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
h2-database
In-memory database
XML
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
commons-dbcp2
commons dependency for connection pooling
XML
<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
XML
<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
Java
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
Java
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
Java
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
Java
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.
Java
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:
Similar Reads
Spring MVC - Model Interface
The Spring Web model-view-controller (MVC) is an open-source framework used to build J2EE web applications. It is based on the Model-View-Controller design pattern and implements the basic features of a core spring framework - Dependency Injection. It is designed around a 'DispatcherServlet' that di
7 min read
Spring MVC - Number Validation
The Spring Web model-view-controller (MVC) is an open-source framework used to build J2EE web applications. It is based on the Model-View-Controller design pattern and implements the basic features of a core spring framework - Dependency Injection. It is designed around a 'DispatcherServlet' that di
9 min read
Spring - MVC Form Checkbox
In this article, we will learn about the Spring MVC Checkbox and Checkboxes tags. We will create a basic Spring MVC project in the Spring tool suite(STS) to create checkboxes using form:checkbox and form:checkboxes tags. 'spring-form.tld' tag library In Spring Framework, we can use Java Server Pages
6 min read
Spring - MVC Form Radio Button
Here, we will learn about the Spring MVC radiobutton and radiobuttons tags. We will create a basic Spring MVC project in the Spring tool suite(STS) to create radiobuttons using form:radiobutton and form:radiobuttons tags. 'spring-form.tld' tag libraryIn Spring Framework, we can use Java Server Pages
8 min read
Spring - MVC Form Drop-Down List
In this article, we will learn about the Spring MVC Dropdown list. We will create a basic Spring MVC project in Spring tool suite(STS) to make a dropdown list using form:select tag. spring-form.tld We can use Java Server Pages (JSPs) as a view component in Spring Framework. To help you implement vie
7 min read
Spring - MVC TextArea
Here we will be learning about the Spring MVC TextArea form tag. We will create a basic Spring MVC project in the Spring tool suite(STS) to get some data from the user using the TextArea form tag. spring-form.tld We can use Java Server Pages (JSPs) as a view component in Spring Framework. To help y
7 min read
Spring - MVC Hidden Field
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. In this
6 min read
Spring - MVC Regular Expression Validation
Regular Expression Validation in Spring MVC can be achieved by using Hibernate Validator which is the implementation of Bean Validation API. Hibernate Validator provides @Pattern annotation which is used for regular expression validation. Syntax: @Pattern(regex="", flag="", message="") private Strin
4 min read
Spring - MVC Password
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. In this
8 min read
Spring - MVC Form Tag Library
Spring Framework provides spring's form tag library for JSP views in Spring's Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
14 min read