• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Dynamic Search Query Implementation in JPA

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic