| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.core.framework.persistence.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 | 0 | public enum QueryByCriteriaType {SELECT, UPDATE, DELETE} |
| 32 | |
|
| 33 | |
public QueryByCriteria(EntityManager entityManager, Criteria criteria) { |
| 34 | 0 | this(entityManager, criteria, QueryByCriteriaType.SELECT); |
| 35 | 0 | } |
| 36 | |
|
| 37 | 0 | public QueryByCriteria(EntityManager entityManager, Criteria criteria, QueryByCriteriaType type) { |
| 38 | 0 | this.entityManager = entityManager; |
| 39 | 0 | this.criteria = criteria; |
| 40 | 0 | this.type = type; |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public Query toQuery() { |
| 44 | 0 | Query query = entityManager.createQuery(criteria.toQuery(type)); |
| 45 | 0 | if (criteria.getSearchLimit() != null) { |
| 46 | 0 | query.setMaxResults(criteria.getSearchLimit()); |
| 47 | |
} |
| 48 | 0 | criteria.prepareParameters(query); |
| 49 | 0 | return query; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public Query toCountQuery() { |
| 53 | 0 | Query query = entityManager.createQuery(criteria.toCountQuery()); |
| 54 | 0 | if (criteria.getSearchLimit() != null) { |
| 55 | 0 | query.setMaxResults(criteria.getSearchLimit()); |
| 56 | |
} |
| 57 | 0 | criteria.prepareParameters(query); |
| 58 | 0 | return query; |
| 59 | |
} |
| 60 | |
|
| 61 | |
} |