mysqlspring-boothibernatespring-data-jpahibernate-native-query

Multiple parameter in JPA query and method


I want to pass multiple parameters around(20) in my JPA method. So is there any way in which I can pass an Object as a parameter in my JPA method? How can I use @Param annotation which can take values from my object and assign it to my native query attributes?


Solution

  • You can get close by using Spel Expressions.

    @Query("select u from User u where u.firstname = :#{#customer.firstname}")
    List<User> findUsersByCustomersFirstname(@Param("customer") Customer customer);
    

    You can pass an object(like Customer) to your query methods and then use its reference to set your query params.

    Check out the official docs for more details