I introduced to you all basic about Hibernate, so you will know in Hibernate, an object implements the SessionFactory interface, which will hold information related to the database connection, the entity to manipulate the database. It’s better if this object is managed by Spring, when we need it we can get it to use immediately. In this tutorial, I will guide you all on how to use Hibernate in the Spring framework.
First, I will create a Maven as an example:
Spring dependency is as follows:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.3</version> </dependency> |
To work with Hibernate, like JPA, we also need to add the spring-orm dependency:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.3</version> </dependency> |
Hibernate dependency:
1 2 3 4 5 |
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>6.1.6.Final</version> </dependency> |
In this tutorial, I will use MySQL database so I will also add MySQL Driver dependency as follows:
1 2 3 4 5 |
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.31</version> </dependency> |
Project Lombok:
1 2 3 4 5 6 |
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> |
For simplicity, in this example, I only define a table containing student information with two columns:
1 2 3 4 5 |
CREATE TABLE `student` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; |
The entity of table student has the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.springhibernate; import java.io.Serializable; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; import lombok.Data; @Table(name = "student") @Entity @Data public class Student implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @Column private String name; } |
For Hibernate, Spring 5 supports only one class to create an object implement SessionFactory interface, that is LocalSessionFactoryBean.
This object allows us to declare information related to the database, the entity so we do not need to have the configuration file for Hibernate anymore.
Here’s the information you need to declare for the LocalSessionFactoryBean object in the Spring container:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.huongdanjava.springhibernate.Student</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> |
In the above declaration, as you can see, we have three properties of the LocalSessionFactoryBean object that need to be declared:
The first attribute is related to the Data Source. The Data Source here we can understand is the management of the connection to the database. We can declare many different types of Data Source as many different organizations implement the javax.sql.DataSource interface.
Here, I declare the DataSource using the DriverManagerDataSource object as follows:
1 2 3 4 5 6 |
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/spring_hibernate" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> |
The second attribute we need to declare is the information related to the entity. In this tutorial, I only define one entity, Student.
The final attribute is some of the attributes related to Hibernate. We will use the dialect of MySQL, the MySQLDialect, and will display the SQL that Hibernate uses to execute the database.
So we have finished declaring the SessionFactory interface implementation in the Spring container. Now, just using it: D
My example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.springhibernate; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.query.Query; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); SessionFactory sf = (SessionFactory) ac.getBean("sessionFactory"); Session session = sf.openSession(); Query<Student> q = session.createQuery("From Student "); List<Student> resultList = q.list(); for (Student next : resultList) { System.out.println("Student name: " + next.getName()); } } } |
Currently, the database has the following data:
then when running, the result will be: