How to have order by on 2 fields (belonging to 2 separate entities) using criteria builder in java? -


i have checklist instance object , inside 1 checklist instance there multiple task objects (one-to-many relationship). using criteriabuilder retrieve checklist objects(which includes tasks well). want fetch checklists in order (say on basis of it's created date, ascending order) , want fetch tasks inside each checklist in specific order (say on basis of it's due date, descending). know how fetch the checklist object in order using criteria order expressions shown in example below. want know how fetch tasks each checklist in required fashion ?

 return jpaapi.withtransaction(em -> {     criteriabuilder cb = em.getcriteriabuilder();     criteriaquery<checklistinstance> q = cb.createquery(checklistinstance.class);     root<checklistinstance> instance = q.from(checklistinstance.class);     list<predicate> predicates = new arraylist<>();     predicates.add(cb.equal(instance.get(taskconstants.checklist).get("id"), criteria.getchecklist()));     q.orderby(cb.asc(instance.get(taskconstants.due_date)));     q.select(instance).where(predicates.toarray(new predicate[] {}));     return em.createquery(q).getresultlist();  });   @entity  @table(name = "tm_checklist_instance")  public class checklistinstance implements serializable {       private static final long serialversionuid = 1l;       @id      @generatedvalue(strategy = generationtype.identity)      private long id;       private string guid;       private string name;       @onetomany(targetentity = task.class, mappedby = "checklistinstance", cascade = cascadetype.refresh, fetch = fetchtype.eager)      private list<task> tasks;  } 

add @orderby annotation on task list:

@onetomany(targetentity = task.class, mappedby = "checklistinstance", cascade = cascadetype.refresh, fetch = fetchtype.eager) @orderby("due_date desc")  private list<task> tasks; 

replace "due_date" own field.

hope ;)


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -