Пример #1
0
 @Override
 @SuppressWarnings("rawtypes")
 public int getCount(String jpql, T entity) throws DaoExceptions {
   if (jpql == null || jpql.length() <= 0) {
     jpql = "select count(o) from " + this.entityName + " o";
   }
   final Map map = JPAHelper.buildReflectJpql(jpql, entity);
   final Object[] params = JPAHelper.getParams(map);
   String JPQL = JPAHelper.getJpql(map);
   JPQL = JPQL.substring(0, JPQL.lastIndexOf("order"));
   final Query query = this.entityManager.createQuery(JPQL);
   if ((null != params) && (params.length > 0)) {
     for (int i = 0; i < params.length; i++) {
       query.setParameter(i + 1, params[i]);
     }
   }
   return Integer.parseInt(query.getSingleResult() + "");
 }
Пример #2
0
 @Override
 @SuppressWarnings({"rawtypes", "unchecked"})
 public List<T> getCollection(String jpql, T entity, String[] attributes, boolean pageFlag)
     throws DaoExceptions {
   if (jpql == null || jpql.length() <= 0) {
     jpql = "select o from " + this.entityName + " o";
   }
   if (!JPAHelper.validJpql(jpql)) {
     throw new DaoExceptions("JPQL语句不合法,请核实!");
   }
   final Map map = JPAHelper.buildReflectJpql(jpql, entity);
   String JPQL = JPAHelper.getJpql(map);
   Object[] params = JPAHelper.getParams(map);
   if (attributes != null && attributes.length > 0) {
     StringBuilder builder = new StringBuilder();
     builder.append(" order by");
     for (String attribute : attributes) {
       builder.append(" o." + attribute + " ,");
     }
     String orderString = builder.substring(0, builder.lastIndexOf(",")) + " desc";
     JPQL = JPQL.substring(0, JPQL.lastIndexOf("order")) + orderString;
   }
   final Query query = entityManager.createQuery(JPQL);
   if (null != params && params.length > 0) {
     for (int i = 0; i < params.length; i++) {
       query.setParameter(i + 1, params[i]);
     }
   }
   List<T> entities = new ArrayList<T>();
   if (pageFlag) {
     entities =
         query
             .setFirstResult(entity.getEndSize())
             .setMaxResults(entity.getPageSize())
             .getResultList();
   } else {
     entities = query.getResultList();
   }
   return entities;
 }