jpa 2.0 - JPA Query to select based on criteria alongwith pagination -


i have submission table id,name,code among other properties. requirement search records based on mentioned properties , return paginated set.

this pseudocode looking for

searchsubmission(searchfilter sf,pageindex,noofrecords) {    query = 'from submisssion code=sf.code or id=sf.id order id start_from (pageindex*noofrecords) limit noofrecords'    return result();  } 

there seem lot of options: criteriabuilder,namedquery etc.. efficient 1 in situation?

for jpa query objects (except native sql queries), use pagination through setmaxresults(int) , setfirstresult(int) methods. instance:

  return em.createnamedquery("yourqueryname", yourentity.class)       .setmaxresults(noofrecords)       .setfirstresult(pageindex * noofrecords));       .getresultlist(); 

jpa perform pagination you.

named queries predefined , can cached, while other types dynamically created.
choice use jpql like:

query query = em.createquery("select s submission s s.code = :code or s.id = :id order s.id", submission.class); 

or criteriabuilder api form similar query:

    criteriabuilder qb = em.getcriteriabuilder();     criteriaquery<submission> cq = qb.createquery(submission.class);      root<submission> root = cq.from(submission.class);     cq.where( qb.or(          qb.equal(root.get("code"), qb.parameter(string.class, "code")),         qb.equal(root.get("id"), qb.parameter(integer.class, "id"))     ));     query query = em.createquery(cq); 

don't forget set parameter values using query.setparameter("id", sf.id) example.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -