Trong bài viết trước, mình đã giới thiệu với các bạn tổng quan về Jakarta Persistence Query Language để thao tác với các loại database sử dụng các entity. Mỗi khi các bạn cần thông tin gì từ database, các bạn sẽ sử dụng các câu lệnh SELECT hoặc UPDATE hoặc DELETE cùng với tên entity để build thành câu query, sau đó sẽ lấy đối tượng Query từ Entity Manager sử dụng câu query này, và cuối cùng là gọi một phương thức thích hợp tuỳ theo nhu cầu của các bạn từ đối tượng Query này để lấy kết quả mà các bạn mong muốn. Rất nhiều bước cần phải làm, phải không các bạn? Nếu các bạn sử dụng Jakarta Persistence cùng với Spring Framework trong dự án của mình, thì hãy thử sử dụng Spring Data JPA nhé! Nó sẽ giúp các bạn giảm thiểu rất nhiều các công việc phải làm trong đa số các trường hợp cần thao tác với database đó các bạn. Cụ thể như thế nào? Trong bài viết này, chúng ta hãy cùng nhau tìm hiểu về Spring Data JPA, các bạn nhé!
Đầu tiên, mình sẽ tạo mới một Maven project để làm ví dụ:

Chúng ta sẽ sử dụng Java 25 cho ví dụ này:
|
1 2 3 4 |
<properties> <maven.compiler.source>25</maven.compiler.source> <maven.compiler.target>25</maven.compiler.target> </properties> |
Spring Data JPA dependency:
|
1 2 3 4 5 |
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>4.1.2</version> </dependency> |
Ở đây, mình sẽ sử dụng Jakarta Persistence với implementation của Hibernate nên mình sẽ thêm Hibernate dependency như sau:
|
1 2 3 4 5 |
<dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <version>7.4.14.Final</version> </dependency> |
Project Lombok:
|
1 2 3 4 5 6 |
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.48</version> <scope>provided</scope> </dependency> |
Trong bài viết này, mình sẽ sử dụng MySQL database:
|
1 2 3 4 5 |
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>26.10.0</version> </dependency> |
Để đơn giản, mình chỉ định nghĩa 1 table chứa thông tin sinh viên với 2 cột như sau:
|
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; |
Sample data:

Entity của table này:
|
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.springdatajpa; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; import lombok.Getter; import lombok.Setter; @Table(name = "students") @Entity @Getter @Setter public class Student { @Id @GeneratedValue private Long id; @Column private String name; } |
Mình sẽ khai báo EntityManagerFactory trong Spring container sử dụng tập tin spring.xml:

như sau:
|
1 2 3 4 5 6 7 |
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="packagesToScan" value="com.huongdanjava.springdatajpa"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> </bean> |
với datasource:
|
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:3307/example" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> |
OK, vậy là đã chuẩn bị mọi thứ xong rồi đó các bạn. Các bạn có thể tham khảo thêm bài viết về JPA và Spring Framework tại đây nhé!
Giờ chúng ta đi vào chủ đề chính của bài viết này thôi.
Điều đầu tiên, các bạn cần phải biết là Spring Data JPA là một module nhỏ trong một project lớn gọi là Spring Data Project. Mục đích của Spring Data project là giảm thiểu các đoạn code lặp đi lặp lại liên quan đến phần thao tác với các hệ thống quản trị data khi phát triển các ứng dụng có sử dụng Spring Framework. Ngoài Spring Data JPA hỗ trợ cho Jakarta Persistence giảm thiểu code để truy cập và thao tác với các hệ thống quản trị cơ cở dữ liệu relational, chúng ta còn có Spring Data JDBC (cũng giống như Spring Data JPA), Spring Data LDAP (hỗ trợ Spring LDAP), Spring Data MongoDB (hỗ trợ cho MongoDB),… Các bạn có thể tìm thấy đầy đủ các module của Spring Data tại đây.
Để đạt được mục đích giảm thiểu code như mình đã nói, Spring Data định nghĩa một interface chính tên là Repository nằm trong module Spring Data Common. Module này sẽ được sử dụng cho tất cả các module còn lại trong Spring Data project. Nội dung của interface này đơn giản như sau:
|
1 2 3 4 5 6 7 8 |
package org.springframework.data.repository; import org.springframework.stereotype.Indexed; @Indexed public interface Repository<T, ID> { } |
Interface này sử dụng 2 generic type:
- T là domain class mà repository sẽ quản lý
- ID là kiểu dữ liệu của ID của domain class mà repository quản lý.
Vì interface này đơn giản như vậy, nên chắc chắn nó phải có những interface khác extend nó để thể hiện Spring Data project làm được những gì phải không các bạn? 😀
Ở đây, chúng ta sẽ có nhiều interface khác extend từ interface Repository, tùy thuộc vào module mà chúng ta sử dụng. Do chúng ta đang thảo luận về Spring Data JPA, nên mình sẽ chỉ liệt kê ở đây 2 interface extend interface Repository mà Spring Data JPA đang sử dụng. Đó chính là interface CrudRepository và PagingAndSortingRepository.
Interface CrudRepository với ý nghĩa create, read, update, delete cho phép chúng ta thực hiện các thao tác cơ bản đến với các hệ thống data (hệ thống data ở đây các bạn phải hiểu theo nghĩa rộng, nghĩa là nó không chỉ là database đâu nhé các bạn). Còn interface PagingAndSortingRepository sẽ hỗ trợ việc phân trang và sắp xếp cho Spring Data JPA. 2 interface CrudRepository và PagingAndSortingRepository đều có thêm extension để hỗ trợ cho List thay vì Iterable là ListCrudRepository và ListPagingAndSortingRepository các bạn nhé! Tất cả các interface mà mình vừa kể trên đều nằm trong module Spring Data Common.
Spring Data JPA module định nghĩa một interface là JpaRepository kế thừa interface ListCrudRepository, ListPagingAndSortingRepository và QueryByExampleExecutor.
QueryByExampleExecutor là môt interface khác của Spring Data Common module nha các bạn. Nó cung cấp thêm tính năng giúp chúng ta có thể search các entity dựa vào các non-null properties của các entity này. Mình sẽ đề cập chi tiết về QueryByExampleExecutor trong các bài viết sau các bạn nhé!
Với việc extend từ interface ListCrudRepository, ListPagingAndSortingRepository, các bạn cũng có thể hình dung là Spring Data JPA có thể giúp chúng ta giảm thiểu code cho các thao tác nào liên quan đến database rồi phải không các bạn? Có thể kể ra đây là: create, read, update, delete, paging và sorting. 😀
Các bạn có thể thấy rõ cấu trúc extend cho những interface trên bằng hình ảnh sau:

Có rất nhiều các query method được hỗ trợ mặc định bởi các interface CrudRepository, PagingAndSortingRepository, ListCrudRepository, ListPagingAndSortingRepository, QueryByExampleExecutor và cả JpaRepository, các bạn chỉ cần sử dụng để thao tác với database mà thôi. Spring Data JPA sẽ đảm nhận các công việc liên quan đến build câu query và execute câu query để lấy kết quả về cho chúng ta đó các bạn.
OK, bây giờ hãy cùng mình làm một ví dụ với interface JpaRepository xem nó hoạt động như thế nào nhé các bạn.
Mình sẽ tạo mới một interface tên là StudentRepository extend từ JpaRepository với nội dung như sau:
|
1 2 3 4 5 6 7 |
package com.huongdanjava.springdatajpa; import org.springframework.data.jpa.repository.JpaRepository; public interface StudentRepository extends JpaRepository<Student, Long> { } |
Domain class ở đây chính là tên entity Student còn Long chính là kiểu dữ liệu của properties trong entity Student mapping với column primary key trong table students nha các bạn.
Chúng ta sẽ khai báo interface này trong Spring container như sau:
|
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 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="com.huongdanjava.springdatajpa"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="packagesToScan" value="com.huongdanjava.springdatajpa"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> </bean> <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:3307/example"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> </beans> |
Ở đây, mình sử dụng tag <jpa:repositories> với base package là com.huongdanjava.springdatajpa để scan StudentRepository interface. Với khai báo như trên, Spring Data JPA sẽ tự động tạo đối tượng Repository Proxy để lấy đối tượng implement cho interface StudentRepository là SimpleJpaRepository, như các bạn thấy trong hình ảnh ở trên, để khởi tạo trong Spring container.
Vì đối tượng implement SimpleJpaRepository sử dụng transaction nên các bạn cần phải khai báo thêm bean transactionManager vào Spring container các bạn nhé:
|
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 26 27 28 29 30 31 32 33 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <jpa:repositories base-package="com.huongdanjava.springdatajpa"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="packagesToScan" value="com.huongdanjava.springdatajpa"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> </bean> <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:3307/example"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <tx:annotation-driven proxy-target-class="true"/> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> </beans> |
OK, đến đây là chúng ta đã hoàn thành những phần cấu hình cần thiết rồi. Giờ chạy ví dụ thử nhé các bạn. Class main để chạy ứng dụng của mình như sau:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.springdatajpa; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); StudentRepository studentRepository = (StudentRepository) ac.getBean("studentRepository"); studentRepository.findById(1L).ifPresent(s -> System.out.println(s.getName())); } } |
Kết quả sẽ là:

Giả sử giờ chúng ta thêm một query method trong interface StudentRepository có nội dung như sau:
|
1 2 3 4 5 6 7 8 |
package com.huongdanjava.springdatajpa; import org.springframework.data.jpa.repository.JpaRepository; public interface StudentRepository extends JpaRepository<Student, Long> { Student findByName(String name); } |
thì khi chạy ví dụ sau:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.springdatajpa; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); StudentRepository studentRepository = (StudentRepository) ac.getBean("studentRepository"); Student student = studentRepository.findByName("Khanh Nguyen"); System.out.println(student.getId()); } } |
kết quả sẽ là:

Như các bạn thấy, mặc dù không có bất kỳ class nào implement interface StudentRepository nhưng chúng ta vẫn truy vấn table students theo column name được. Giảm được rất nhiều code mà chúng ta cần phải làm ,phải không các bạn?

Vũ Hội
Em chào Anh !
1. Theo như e hiểu thì JPA là một 1 đặc tả của ORM. Nó bảo gồm các Interface không triển khai. Hay nói cách khác JPA định nghĩa các đặc tả cần thiết và không có code thực thi những đặc tả đó. Còn Hibernate sẽ thực hiện chi tiết các đặc tả của JPA. Nếu coi JPA là 1 Interface thì Hibernate sẽ Implement nó. Em hiểu như vậy có đúng không ạ ?
2. Spring Data JPA là gì ? A có thể giải thích giúp e được không ạ ? E đọc mà vẫn thấy mơ hồ về nó !
Khanh Nguyen
1. Đúng rồi đó em. Bổ sung thêm là Hibernate còn có một implementation riêng của nó để thao tác với database.
2. Spring Data JPA là một framework giúp chúng ta dễ dàng, nhanh chóng cấu hình các thông tin cần thiết để kết nối và thao tác với database, như các query method chẳng hạn,…
namns
trong jpa có sẵn các crud, còn hibernate sẽ có thêm nhiều phương thức hơn. Như vậy nói chỉ gồm các interface không có code thực thi hình như là sai!
hibernate là thể hiện mở rộng hơn của JPA
nguyễn tuyên hoàng
anh ơi. các column thì có phải khai báo tên tương ứng vs database ko a. Ví dụ như sau @Id em thêm @column(name=”id”) có đc ko a.
Khanh Nguyen
Em đọc thêm về JPA nhé https://huongdanjava.com/vi/java-persistence-api
Nhan
a !.. cho e xin fb hay zì dó e hỏi a chi tiết phần spring JPA vs ạ . thanks a
Khanh Nguyen
Post câu hỏi vào này nè e https://www.facebook.com/groups/721409674679531/?ref=bookmarks
Java
lam sao de spring hieu dc ham findByName trong HelloRespository ban nhi
Java
import org.springframework.data.annotation.Id; trong Student co ve bi sai.minh thay the bang import javax.persistence.*; thi ok
lam
anh cho em hỏi trong JpaRepository có phương thức getOne() nhưng em dùng thì nó báo lỗi, còn findById() thì dùng bình thường, thế khi nào thì dùng getOne() ạ ?
Khanh Nguyen
getOne() chỉ nên được sử dụng nếu em chỉ cần check xem record với thông tin em đang truyền có tồn tại trong database hay không? Nó ko query xuống database mà chỉ check trong EntityManager có entity với thông tin mà em đang truyền hay không? Sử dụng getOne() khi em ko cần đọc thông tin của entity nhé!
Tuân
Lúc lưu thì làm sao a nhỉ? Em dùng hàm save mà nó báo lỗi “Incorrect syntax near ‘hibernate_sequence’.”
Khanh Nguyen
Anh nghĩ em đang khai báo entity bị sai đó? Check lại thử.
Hau pham
hi a,
e lam theo huong dan cua anh, cai entity cua no bao loi nhu vay
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.pdhau.springdatajpa.Shape
khong biet fix nhu the nao mong a giup
Hau pham
voi lai e dau thay cho nao create cai bean “helloRepository” dau? ma sao minh co the get bean ra duoc t context vay a?
Khanh Nguyen
Implementation của interface HelloRepository là class SimpleJpaRepository đó em.
Khanh Nguyen
Code của em ra sao nhỉ? Anh nghĩ phải có khác gì đó.
meomun
hi anh public interface BookRepository extends CrudRepository{
}
public interface BookRepository extends jpaRepository{
}
hai interface trên đều cho kết quả giống nhau vậy nó có khác nhau ko anh
Khanh Nguyen
Khác chứ em 😀
Cái CrudRepository là interface dùng chung cho các Spring Data project, còn JpaRepository là cho Spring Data JPA. Em xem nội dung của những interface này để thấy rõ nhé.