How to use SpEL (Spring Expression Language) in the Spring Data JPA

Imran Shaikh
0

An expression language is used to query and operate on the objects at runtime. In Java, several expression languages are available to use, such as JBoss EL, MVFLEX Expression Language (MVEL),Object-Graph Navigation Language (ONGL) etc.


In this article, we will have some hands-on SpEL (Spring Expression Language), and I will walk you through how to use SpEL in the Spring Data JPA @Query annotation.


toc
How to use SpEL (Spring Expression Language) in the Spring Data JPA thumbnail

SpEL in the Spring Data JPA


The Spring Data Jpa enables us to manually bind the parameters using the @Query annotation and this @Query annotation is also capable of binding the dynamic parameters using the SpEL (Spring Express Language).


There are a couple of approaches to hook the SpEL parameter in the Spring Data JPA repository methods. It can be an index base entry or a named parameter utilizing the @Param annotation.


Index approach

In this approach, we hook the parameters using the indexes of the method argument. The following is a simple example of SpEL in the Spring data Jpa used as the index of the method argument.


 @Query("FROM BookEntity WHERE uniqueCode = ?#{[0]} AND title = ?#{[1]}")
 List<BookEntity> findBookByUniqueBookCode(String uniqueCode, String title); 

Using the named parameter in the @Param annotation

The SpEL(Spring Expression Language) can be binded using the @Param annotation to the @Query. The @Param annotation expects the value argument. The SpEL declared using the @Param annotation is shown below.


 @Query("FROM BookEntity WHERE uniqueCode = ?#{#uniqueCode} AND title = ?#{#title}")
 List<BookEntity> findBookByUniqueBookCode1(@Param("uniqueCode") String uniqueCode, @Param("title") String title);

Various patterns of initiating SpEL (Spring Expression Language)


The Spring Data JPA provides two patterns to bind the SpEL parameters in the @Query annotation. The ?# or :# can be used to activate the SpEL binding.


We looked at how to bind the SpEL parameters using the ?#. I prefer the second syntax, which is :#. In the subsequent examples, I will lead you how to use :#.



The SpEL (Spring Expression Language) utilizing the entity class.


Are you wondering if we can use named parmaters using the @param annotion without using SpEL and this will work perfectly fine? Then why should I use SpEL?


Correct. The real joy of using SpEL is with the entity class. Allow me to present to you how we can take advantage of the SpEL using the entity classes. The below snippet utilises the enity in the method argument.


 @Query("SELECT price FROM BookEntity where uniqueCode = :#{#bookEntity.uniqueCode}")
 List<Double> findBookPriceByUniqueCode(@Param("bookEntity") BookEntity bookEntity); 

I have set only the uniqueCode parameter in the BookEnity because I only require the uniqueCode parameter in the query. You can configure the entity parameters to meet your conditions.


 BookEntity bookEntity = BookEntity.builder()
	.uniqueCode("BK0001")
	.build(); 
 List bookPriceByUniqueCode = bookRepository.findBookPriceByUniqueCode(bookEntity);

The SpEL (Spring Expression Language) with the relationship entity

We frequently work with relational databases, and we may be required to add entities within entities in order to map the associations in Hibernate.


If you are wondering, can we take benefit of SpEL while using the relationship hibernate mapping? The response will be yes, we can take total advantage of the SpEL in the hibernate mapping entity as well.


 @Query("FROM BookEntity WHERE authorEntity = :#{#bookEntity.authorEntity}")
 List<BookEntity> findBooksByAuthor(@Param("bookEntity") BookEntity bookEntity); 

 AuthorEntity authorEntity = AuthorEntity.builder()
	.authorId(1)
	.build();
 BookEntity bookEntity = BookEntity.builder()
	.authorEntity(authorEntity)
	.build(); 
 List findBooksByAuthor = bookRepository.findBooksByAuthor(bookEntity);


Summary


In this tutorial, we learned about expression language and how to utilize SpEL (Spring Express Language) in the Spring Data JPA @Query. and We saw various patterns for binding the parameters in the SpEL and multiple ways to access them in the Spring Data JPA.


If you are curious to read the source code, you can find it here.


Keep learning and keep growing.

(getButton) #text=(Next: Configure Multiple Databases in Spring Data JPA) #icon=(link) #color=(#2339bd)

Post a Comment

0 Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top