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

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay