Query creation trong Spring Data JPA (Đặt tên cho method)

Query creation trong Spring Data JPA (Đặt tên cho method)

(Xem lại: So sánh CrudRepository với JpaRepository trong Spring Data)

Query creation là gì?

Query creation là một cơ chế cho phép tạo ra các câu query theo tên method trong Spring Data JPA. Tức là chỉ cần bạn đặt tên theo đúng chuẩn thì nó sẽ tự động sinh ra các câu query tương ứng và trả về kết quả.

Khi bạn tạo các interface thừa kế từ CrudRepository hoặc JpaRepository là bạn đã có thể sử dụng sẵn các method như findById, findAll...

Còn với những trường hợp muốn truy vấn, select theo các field khác nhau với nhiều điều kiện khác nhau ta có thể chuyển sang dùng @Query trước các method để định nghĩa câu truy vấn.

Có một cách khác là áp dụng query creattion, chỉ cần đặt tên theo đúng chuẩn là nó tự động sinh ra các câu truy vấn tương ứng.

Ví dụ:

public interface UserRepository extends Repository<User, Long> {

  List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
}

Câu truy vấn tương ứng sẽ là:

select u from User u where u.emailAddress = ?1 and u.lastname = ?2

Một số chú ý với query creation khi đặt tên method

  • Tên method có thể bao gồm các biểu thức kết hợp như AND hayOR, hay các biểu thức so sánh như BetweenLessThanGreaterThan, Like.
  • Bạn có thể sử dụng IgnoreCase cho các thuộc tính là String, (Ex:findByLastnameIgnoreCase(…)findByLastnameAndFirstnameAllIgnoreCase(…)).
  • Có thể áp dụng việc sắp xếp kết quả bằng cách nối thêm một mệnh đềOrderBy như Asc hoặc Desc vào tên method (Ví dụ: findAllByOrderByNameDesc())
  • Có thể áp dụng với cả thuộc tính là một đối tượng. Ví dụ đối tượng User có thuộc tính là Address: findByAddress(Address address)

Query creation trong Spring Data JPA (Đặt tên cho method) stackjava.com

Dưới đây là chi tiết cách đặt tên method trong interface Repository với các từ khóa khác nhau

Từ khóa Ví dụ JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Between findByStartDateBetween … where x.startDate between 1? and ?2
LessThan findByAgeLessThan … where x.age < ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection<Age> ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection<Age> age) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false

Query creation trong Spring Data JPA (Đặt tên cho method) stackjava.com

Okay, Done!

 

References:

https://docs.spring.io/spring-data/…/query-creation

https://docs.spring.io/spring-data/…/jpa.repositories.html

 

stackjava.com