Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jacobheric/868205 to your computer and use it in GitHub Desktop.

Select an option

Save jacobheric/868205 to your computer and use it in GitHub Desktop.
A sample hibernate search using criteria, disjunction, projection and paging
public List<Recipe> search(RecipeCriteria recipeCriteria) {
Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Recipe.class);
//
//Property restrictions
if (recipeCriteria.getQuery() != null) {
Disjunction d = Restrictions.disjunction();
d.add(Restrictions.like("name", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
d.add(Restrictions.like("tasteNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
d.add(Restrictions.like("brewNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
c.add(d);
}
//
//Determine the total before limiting (useful of paging
c.setProjection(Projections.rowCount());
recipeCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
c.setProjection(null);
c.setResultTransformer(Criteria.ROOT_ENTITY);
//
//Start & limit grid page restrictions
if (recipeCriteria.getStart() != null){
c.setFirstResult(recipeCriteria.getStart());
}
if (recipeCriteria.getLimit() != null){
c.setMaxResults(recipeCriteria.getLimit());
}
return c.list();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment