java - Spring Data JPA Multiple Optional Search Parameters With Multiple Joins -


i'm using spring boot spring data jpa connect oracle database. 1 parameter use spring repository findbyid(long id); , works great. search on other hand more complicated me. in our case, user provides nested json object multiple optional search parameters (at point can't change way way send data has through nested json). here json input object looks like:

{   "agent_filter": {     "first_name": "string",     "last_name": "string",     "agentnumber": "string",     "agentcode": "string"   },   "account": "string",   "status": "string",   "paid": "string",   "amount": "string",   "person_filter": {     "date_of_birth": "string",     "first_name": "string",     "last_name": "string",     "tax_id": "string"   } } 

all search criteria optional (except @ least 1 parameter)

on back-end have following entities:

@entity account{ @onetomany list<personrole> role;  @onetomany list<agentrole> role; }  @entity personrole{ string role;  person person; }  @entity agentrole{ string role;  agent agent; }  @entity person{...}  @entity agent{...} 

so provide search functionality can multiple joins. started using jpql @query notation had is null or checks each parameter , it's big mess. started looking other options , saw stuff querydsl, criteria, specification wasn't sure 1 should focus on , learn about. unfortunately don't know whole lot on subject , hoping can point me in right direction implementation of search. thank you!

querydsl ftw!

let me give example code when had similar problem in had bunch of stuff wanted filter on , lot of them null...

btw, if need fancy joins you're going use query dsl directly. these example querydsl 3 might have change querydsl 4. because you've mentioned how 'so provide search functionality can multiple joins' you're going need use querydsl directly.

first create , booleanbuilder , this:

booleanbuilder builder = new booleanbuilder(); qcontent content = qcontent.content; if (contentfilter.headlinefilter == null || contentfilter.headlinefilter.trim().length() == 0) {         // no filtering on headline headline filter = null or blank     } else if (contentfilter.headlinefilter.equals(filter.null_string)) {         // special case when want filter specific null headline         builder.and(content.label.isnull());     } else {         try {             long parselong = long.parselong(contentfilter.headlinefilter);             builder.and(content.id.eq(parselong));         } catch (numberformatexception e) {             builder.and(content.label.contains(contentfilter.headlinefilter));         }     }     if (contentfilter.todate != null) {         builder.and(content.modifieddate.loe(contentfilter.todate));     }     if (contentfilter.fromdate != null) {         builder.and(content.modifieddate.goe(contentfilter.fromdate));     } 

so based on whether or not have each field can add filter.

to work you're going need generate query dsl meta data - done com.mysema.query.apt.jpa.jpaannotationprocessor annotation processor. generates qcontent.content stuff above.

that booleanbuilder subclass of predicate.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -