Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Search Query Implementation in JPA

 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am currently working on a Spring + Hibernate project. I am using Spring Data JPA with Hibernate as the provider. I have to implemen a search feature in my application.

Here is the repository



I have to implement this search with 6 parameters but as of now I have implemented with only one. I have generated a dynamic query based on the parameters searched. (ie.)If a parameter is present, I will join the table and insert the where condition. How do I bring the dynamically generated query inside of @Query or should I handle this in a different way?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:How do I bring the dynamically generated query inside of @Query or should I handle this in a different way?

As far as I know the query specified in the @Query annotation must be a constant/literal, because it will be processed during startup. But you can handle it in a different way: check the Advanced Spring Data JPA - Specifications and Querydsl article. Here is another blog article using the same technique.

Hope it helps!
Kind regards,
Roel
 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs,

Thanks for the reply. Sorry I was stuck with some other work so didn't concentrate on this project. I took a look over the links you provided but both of them deal with passing different values to same parameter (from what I understood, correct me if I am wrong), (ie) passing San Fransisco, New york, Chicago etc. to the city parameter but what I want is I have to build a criteria like "SELECT * FROM profile where name=? and fullName=? and title=? and city=? and state=? and country=?". Here as for any search the possibilities are only one parameter will be passed or all 6 may be passed. It has to search and bring back the result.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:I took a look over the links you provided but both of them deal with passing different values to same parameter (from what I understood, correct me if I am wrong), (ie) passing San Fransisco, New york, Chicago etc. to the city parameter but what I want is I have to build a criteria like "SELECT * FROM profile where name=? and fullName=? and title=? and city=? and state=? and country=?". Here as for any search the possibilities are only one parameter will be passed or all 6 may be passed. It has to search and bring back the result.

You are too much focused on what they do instead of looking more to how it's done. It is all about the concepts used in these articles.

Using the Specification API you could create different simple methods like isName, isFullName, isTitle, and so on. And then you can freely combine atomic Specifications to create new ones one on the fly. So if you combine them using a helper class with and() and or() methods, you can combine as many atomic methods as you want and build dynamic search queries.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic