List of usage examples for javax.persistence.metamodel SingularAttribute getType
Type<T> getType();
From source file:org.apache.openejb.util.proxy.QueryProxy.java
private <T> Query createFinderQuery(final EntityManager entityManager, final String methodName, final Class<T> entityType, final Object[] args) { final List<String> conditions = parseMethodName(methodName); final EntityType<T> et = entityManager.getMetamodel().entity(entityType); final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Object> query = cb.createQuery(); final Root<T> from = query.from(entityType); query = query.select(from);/*from w ww . java 2 s. co m*/ int i = 0; Predicate where = null; for (final String condition : conditions) { final SingularAttribute<? super T, ?> attribute = et.getSingularAttribute(condition); final Path<?> path = from.get(attribute); final Class<?> javaType = attribute.getType().getJavaType(); final Predicate currentClause; if (javaType.equals(String.class)) { currentClause = cb.like((Expression<String>) path, (String) args[i++]); } else if (Number.class.isAssignableFrom(javaType) || javaType.isPrimitive()) { currentClause = cb.equal(path, args[i++]); } else { LOGGER.warning("field " + condition + " not found, ignoring"); continue; } if (where == null) { where = currentClause; } else { where = cb.and(where, currentClause); } } if (where != null) { query = query.where(where); } // pagination final TypedQuery<?> emQuery = entityManager.createQuery(query); if (args != null && args.length == conditions.size() + 2 && isInt(args[args.length - 2].getClass()) && isInt(args[args.length - 1].getClass())) { final int first = (Integer) args[args.length - 2]; final int max = (Integer) args[args.length - 1]; emQuery.setFirstResult(first); emQuery.setMaxResults(max); } return emQuery; }