DEV Community

Cover image for 🎈Using param object in Spring 🍃 Data JPA query
Jan Cizmar Subscriber for Tolgee

Posted on • Edited on

10 2

🎈Using param object in Spring 🍃 Data JPA query

Spring Boot JPA repositories are very simple and useful approach how to get data we need for our business logic. Instead of implementing methods, which would execute queries, we can just annotate repository methods with @Query annotation. Spring automatically implements these methods using proxies and we don't have to care, what happens under the hood.

We can simply pass variables by defining them as parameters of the annotated methods and using them in the query with : prefix.



  @Query("from Project p where p.user = :userAccount")
  fun getAllUserProjects(userAccount: UserAccount): List<Project>


Enter fullscreen mode Exit fullscreen mode

This is great, but what we need to pass more params to the query, like more complex filters? Can we do that using object encapsulating these parameters? Yes we can! Spring supports also using SpEL in the JPA Queries!



  @Query("""from UserAccount 
    where u.firstname = :#{#customer.firstname}""")
  fun findCoolUsers(
    @Param("customer")
    customer: UserAccount
  ): List<UserAccount>


Enter fullscreen mode Exit fullscreen mode

This is very useful, when we need to pass many parameters to our query and we don't want to define many function parameters.


Tolgee is an open-source solution for software localization. It saves developer's time. Go to Tolgee.io and have fun!

Image description

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video