packagecom.platform.framework.dao.jpa;
importjava.io.Serializable;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjavax.persistence.EntityManager;
importjavax.persistence.criteria.CriteriaBuilder;
importjavax.persistence.criteria.CriteriaBuilder.In;
importjavax.persistence.criteria.CriteriaQuery;
importjavax.persistence.criteria.Order;
importjavax.persistence.criteria.Predicate;
importjavax.persistence.criteria.Root;
importorg.apache.log4j.Logger;
/**
* Query基类
*
* @describe:封装JPA CriteriaBuilder查询条件
* @author:lry
* @since:2014-05-23
*/
@SuppressWarnings({"unused","unchecked","rawtypes","null","hiding"})
publicclassQueryimplementsSerializable {
privatestaticfinallongserialVersionUID = 5064932771068929342L;
privatestaticLogger log = Logger.getLogger(Query.class);
privateEntityManager entityManager;
/** 要查询的模型对象 */
privateClass clazz;
/** 查询条件列表 */
privateRoot from;
privateList predicates;
privateCriteriaQuery criteriaQuery;
privateCriteriaBuilder criteriaBuilder;
/** 排序方式列表 */
privateList orders;
/** 关联模式 */
privateMap subQuery;
privateMap linkQuery;
privateString projection;
/** 或条件 */
privateList orQuery;
privateString groupBy;
privateQuery() {
}
privateQuery(Class clazz, EntityManager entityManager) {
this.clazz = clazz;
this.entityManager = entityManager;
this.criteriaBuilder =this.entityManager.getCriteriaBuilder();
this.criteriaQuery = criteriaBuilder.createQuery(this.clazz);
this.from = criteriaQuery.from(this.clazz);
this.predicates =newArrayList();
this.orders =newArrayList();
}
/** 通过类创建查询条件 */
publicstaticQuery forClass(Class clazz, EntityManager entityManager) {
returnnewQuery(clazz, entityManager);
}
/** 增加子查询 */
privatevoidaddSubQuery(String propertyName, Query query) {
if(this.subQuery ==null)
this.subQuery =newHashMap();
if(query.projection ==null)
thrownewRuntimeException("子查询字段未设置");
this.subQuery.put(propertyName, query);
}
privatevoidaddSubQuery(Query query) {
addSubQuery(query.projection, query);
}
/** 增关联查询 */
publicvoidaddLinkQuery(String propertyName, Query query) {
if(this.linkQuery ==null)
this.linkQuery =newHashMap();
this.linkQuery.put(propertyName, query);
}
/** 相等 */
publicvoideq(String propertyName, Object value) {
if(isNullOrEmpty(value))
return;
this.predicates.add(criteriaBuilder.equal(from.get(propertyName), value));
}
privatebooleanisNullOrEmpty(Object value) {
if(valueinstanceofString) {
returnvalue ==null||"".equals(value);
}
returnvalue ==null;
}
publicvoidor(List propertyName, Object value) {
if(isNullOrEmpty(value))
return;
if((propertyName ==null) || (propertyName.size() ==0))
return;
Predicate predicate = criteriaBuilder.or(criteriaBuilder.equal(from.get(propertyName.get(0)), value));
for(inti =1; i
predicate = criteriaBuilder.or(predicate, criteriaBuilder.equal(from.get(propertyName.get(i)), value));
this.predicates.add(predicate);
}
publicvoidorLike(List propertyName, String value) {
if(isNullOrEmpty(value) || (propertyName.size() ==0))
return;
if(value.indexOf("%") <0)
value = "%"+ value +"%";
Predicate predicate = criteriaBuilder.or(criteriaBuilder.like(from.get(propertyName.get(0)), value.toString()));
for(inti =1; i
predicate = criteriaBuilder.or(predicate, criteriaBuilder.like(from.get(propertyName.get(i)), value));
this.predicates.add(predicate);
}
/** 空 */
publicvoidisNull(String propertyName) {
this.predicates.add(criteriaBuilder.isNull(from.get(propertyName)));
}
/** 非空 */
publicvoidisNotNull(String propertyName) {
this.predicates.add(criteriaBuilder.isNotNull(from.get(propertyName)));
}
/** 不相等 */
publicvoidnotEq(String propertyName, Object value) {
if(isNullOrEmpty(value)) {
return;
}
this.predicates.add(criteriaBuilder.notEqual(from.get(propertyName), value));
}
/**
* not in
*
* @param propertyName
* 属性名称
* @param value
* 值集合
*/
publicvoidnotIn(String propertyName, Collection value) {
if((value ==null) || (value.size() ==0)) {
return;
}
Iterator iterator = value.iterator();
In in = criteriaBuilder.in(from.get(propertyName));
while(iterator.hasNext()) {
in.value(iterator.next());
}
this.predicates.add(criteriaBuilder.not(in));
}
/**
* 模糊匹配
*
* @param propertyName
* 属性名称
* @param value
* 属性值
*/
publicvoidlike(String propertyName, String value) {
if(isNullOrEmpty(value))
return;
if(value.indexOf("%") <0)
value = "%"+ value +"%";
this.predicates.add(criteriaBuilder.like(from.get(propertyName), value));
}
/**
* 时间区间查询
*
* @param propertyName
* 属性名称
* @param lo
* 属性起始值
* @param go
* 属性结束值
*/
publicvoidbetween(String propertyName, Date lo, Date go) {
if(!isNullOrEmpty(lo) && !isNullOrEmpty(go)) {
this.predicates.add(criteriaBuilder.between(from.get(propertyName), lo, go));
}
// if (!isNullOrEmpty(lo) && !isNullOrEmpty(go)) {
// this.predicates.add(criteriaBuilder.lessThan(from.get(propertyName),
// new DateTime(lo).toString()));
// }
// if (!isNullOrEmpty(go)) {
// this.predicates.add(criteriaBuilder.greaterThan(from.get(propertyName),
// new DateTime(go).toString()));
// }
}
publicvoidbetween(String propertyName, Number lo, Number go) {
if(!(isNullOrEmpty(lo)))
ge(propertyName, lo);
if(!(isNullOrEmpty(go)))
le(propertyName, go);
}
/**
* 小于等于
*
* @param propertyName
* 属性名称
* @param value
* 属性值
*/
publicvoidle(String propertyName, Number value) {
if(isNullOrEmpty(value)) {
return;
}
this.predicates.add(criteriaBuilder.le(from.get(propertyName), value));
}
/**
* 小于
*
* @param propertyName
* 属性名称
* @param value
* 属性值
*/
publicvoidlt(String propertyName, Number value) {
if(isNullOrEmpty(value)) {
return;
}
this.predicates.add(criteriaBuilder.lt(from.get(propertyName), value));
}
/**
* 大于等于
*
* @param propertyName
* 属性名称
* @param value
* 属性值
*/
publicvoidge(String propertyName, Number value) {
if(isNullOrEmpty(value)) {
return;
}
this.predicates.add(criteriaBuilder.ge(from.get(propertyName), value));
}
/**
* 大于
*
* @param propertyName
* 属性名称
* @param value
* 属性值
*/
publicvoidgt(String propertyName, Number value) {
if(isNullOrEmpty(value)) {
return;
}
this.predicates.add(criteriaBuilder.gt(from.get(propertyName), value));
}
/**
* in
*
* @param propertyName
* 属性名称
* @param value
* 值集合
*/
publicvoidin(String propertyName, Collection value) {
if((value ==null) || (value.size() ==0)) {
return;
}
Iterator iterator = value.iterator();
In in = criteriaBuilder.in(from.get(propertyName));
while(iterator.hasNext()) {
in.value(iterator.next());
}
this.predicates.add(in);
}
/** 直接添加JPA内部的查询条件,用于应付一些复杂查询的情况,例如或 */
publicvoidaddCriterions(Predicate predicate) {
this.predicates.add(predicate);
}
/**
* 创建查询条件
*
* @return JPA离线查询
*/
publicCriteriaQuery newCriteriaQuery() {
criteriaQuery.where(predicates.toArray(newPredicate[0]));
if(!isNullOrEmpty(groupBy)) {
criteriaQuery.groupBy(from.get(groupBy));
}
if(this.orders !=null) {
criteriaQuery.orderBy(orders);
}
addLinkCondition(this);
returncriteriaQuery;
}
privatevoidaddLinkCondition(Query query) {
Map subQuery = query.linkQuery;
if(subQuery ==null)
return;
for(Iterator queryIterator = subQuery.keySet().iterator(); queryIterator.hasNext();) {
String key = (String) queryIterator.next();
Query sub = (Query) subQuery.get(key);
from.join(key);
criteriaQuery.where(sub.predicates.toArray(newPredicate[0]));
addLinkCondition(sub);
}
}
publicvoidaddOrder(String propertyName, String order) {
if(order ==null|| propertyName ==null)
return;
if(this.orders ==null)
this.orders =newArrayList();
if(order.equalsIgnoreCase("asc"))
this.orders.add(criteriaBuilder.asc(from.get(propertyName)));
elseif(order.equalsIgnoreCase("desc"))
this.orders.add(criteriaBuilder.desc(from.get(propertyName)));
}
publicvoidsetOrder(String propertyName, String order) {
this.orders =null;
addOrder(propertyName, order);
}
publicClass getModleClass() {
returnthis.clazz;
}
publicString getProjection() {
returnthis.projection;
}
publicvoidsetProjection(String projection) {
this.projection = projection;
}
publicClass getClazz() {
returnthis.clazz;
}
publicList getOrders() {
returnorders;
}
publicvoidsetOrders(List orders) {
this.orders = orders;
}
publicEntityManager getEntityManager() {
returnthis.entityManager;
}
publicvoidsetEntityManager(EntityManager em) {
this.entityManager = em;
}
publicRoot getFrom() {
returnfrom;
}
publicList getPredicates() {
returnpredicates;
}
publicvoidsetPredicates(List predicates) {
this.predicates = predicates;
}
publicCriteriaQuery getCriteriaQuery() {
returncriteriaQuery;
}
publicCriteriaBuilder getCriteriaBuilder() {
returncriteriaBuilder;
}
publicvoidsetFetchModes(List fetchField, List fetchMode) {
}
publicString getGroupBy() {
returngroupBy;
}
publicvoidsetGroupBy(String groupBy) {
this.groupBy = groupBy;
}
}