Home > Mobile >  How to mapping Map to query in Repository in Spring Data Jpa?
How to mapping Map to query in Repository in Spring Data Jpa?

Time:05-16

How to mapping Map to query in Repository in Spring Data Jpa?

Below is my Generic Repository source.

@NoRepositoryBean
public interface GenericRepository<T, ID> extends JpaRepository<T, ID> {

     @Query("select t from #{#entityName} t "  
             "where t.queryParams.getKey = queryParmas.getValue")
     Page<T> findByQueryParams(HashMap<String, String> queryParams, Pageable pageable);
}

As follows, I want to receive queryParams as a parameter and map the key and value corresponding to queryParams to the where clause.

Since it is a GenericRepository, it cannot be implemented, and I want to put the contents of the Map in @Query.

I've been searching, but I can't find anything about it, so I don't know how to solve this problem.

If there is any way, please let me know.

Best Regards

CodePudding user response:

You might want to use JPA criteria queries https://www.baeldung.com/hibernate-criteria-queries

CodePudding user response:

You need Spring JPA dynamic query. I have prepared a small application that will meet your needs. I hope it will be useful.

github: dynamic query

  • Related