java - QueryDSL: building a query from an entity -
i've started integrating querydsl spring boot project , i'm looking way build query out of existing entity bean. using @modelattribute
it's nice , easy pass in entity via request controller long parameters align bean:
public page<company> getlogins(@modelattribute company company, pageable pageable, @requestparam(value = "page", required = false) string pagenumber){ return companyservice.findbyparameters(company,pagenumber); }
and in service class, can use booleanbuilder build query:
public page<company> findbyparameters(company companysearch,string pagenumber){ qcompany company = qcompany.company; booleanbuilder builder = new booleanbuilder(); if (companysearch.getemail() != null && !companysearch.getemail().equals("")){ builder.and(company.email.eq(companysearch.getemail())); } if (companysearch.getcompanyname() != null && !companysearch.getcompanyname().equals("")){ builder.and(company.companyname.eq(companysearch.getcompanyname())); } //add other clauses... return loginrepository.findall(builder.getvalue(),pageableservice.getpagerequest(pagenumber)); }
..and works fine. seems unnecessary amount of plumbing since i'll have write similar, longwinded conditional code each entity i'm working with. reckon reflection might option, i'm not sure if querydsl has built in handles situation. i've looked @ querydsl docs , nothing jumped out @ me.
so there nice, tidy way of handling situation without clogging service classes boilerplate?
you can use spring data's querydsl
integration. basically, extend querydslpredicateexecutor
in repository interface , add findall
method gets querydsl predicate
, filter results based on predicate
. see more details here.
Comments
Post a Comment