• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

JPA Specifications - dealing with optional parameters

 
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to create dynamic query using JPA specification, but I'm not sure whether I understood the JPA specification properly. I was using https://www.baeldung.com/spring-data-criteria-queries to write the code. The case is, I got some paramters that are optionals, so first I've written all etc alone. like this (from above site):



while scrolling down the site I could read about JPA specifiaction and it looked much more better, because within each parameters I got next if using above solution.

Using JPA specification the code looks like this for now:



As you can see code looks much more better, I got rid of boiler plate, but the problem is, just those advertisements are returned that meet all requirements so it has category/city/proper title.

If I got



and if I put only Warsaw I want it to be returned, if I put only random (as title) I want it to be returned etc etc, for now if I put Warsaw, random (as title), MUSIC Could you tell me how to do it?


PS: The code  I write is kotlin, but it is very similiar to java so you can propose solution in java if you want.
Thanks a lot for help!

//edit

I changed to

   

and it works, but the question is, how to make to return all the advertisements if no parameters where passed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic