1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.jpa.criteria;
17
18 import javax.persistence.EntityManager;
19 import javax.persistence.Query;
20
21
22
23
24
25 public class QueryByCriteria {
26
27 private EntityManager entityManager;
28 private Criteria criteria;
29 private QueryByCriteriaType type;
30
31 public enum QueryByCriteriaType {SELECT, DELETE}
32
33 public QueryByCriteria(EntityManager entityManager, Criteria criteria) {
34 this(entityManager, criteria, QueryByCriteriaType.SELECT);
35 }
36
37 public QueryByCriteria(EntityManager entityManager, Criteria criteria, QueryByCriteriaType type) {
38 this.entityManager = entityManager;
39 this.criteria = criteria;
40 this.type = type;
41 }
42
43 public Query toQuery() {
44 Query query = entityManager.createQuery(criteria.toQuery(type));
45 if (criteria.getSearchLimit() != null) {
46 query.setMaxResults(criteria.getSearchLimit());
47 }
48 criteria.prepareParameters(query);
49 return query;
50 }
51
52 public Query toCountQuery() {
53 Query query = entityManager.createQuery(criteria.toCountQuery());
54 if (criteria.getSearchLimit() != null) {
55 query.setMaxResults(criteria.getSearchLimit());
56 }
57 criteria.prepareParameters(query);
58 return query;
59 }
60
61 }