Skip to content

Instantly share code, notes, and snippets.

@jarod-chan
Created December 12, 2012 08:22
Show Gist options
  • Save jarod-chan/4266061 to your computer and use it in GitHub Desktop.
Save jarod-chan/4266061 to your computer and use it in GitHub Desktop.
MonthChkRepositoryJpa.java
@Override
public List<MonthChkItem> getMonthChkItems(Long year, Person person) {
CriteriaBuilder builder=entityManager.getCriteriaBuilder();
CriteriaQuery<MonthChkItem> query=builder.createQuery(MonthChkItem.class);
Root<MonthChkItem> monthChkItem=query.from(MonthChkItem.class);
Join<MonthChkItem,MonthChk> monthChk = monthChkItem.join("monthChk",JoinType.LEFT);
List<Predicate> criteria=new ArrayList<Predicate>();
if(year!=null){
criteria.add(builder.equal(monthChk.get("year"), year));
}
if(person!=null){
criteria.add(builder.equal(monthChk.get("person"), person));
}
if(criteria.size()==1){
query.where(criteria.get(0));
}else{
query.where(builder.and(criteria.toArray(new Predicate[0])));
}
query.orderBy(
builder.asc(monthChk.get("year")),
builder.asc(monthChk.get("month")),
builder.asc(monthChkItem.get("sn"))
);
return entityManager.createQuery(query).getResultList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment