Created
March 13, 2011 16:15
-
-
Save jacobheric/868205 to your computer and use it in GitHub Desktop.
A sample hibernate search using criteria, disjunction, projection and paging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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