private <T> Query createFinderQuery(EntityManager entityManager, String methodName, Class<T> entityType, 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();
Root<T> from = query.from(entityType);
query = query.select(from);
int i = 0;
Predicate where = null;
for (String condition : conditions) {
SingularAttribute<? super T, ?> attribute = et.getSingularAttribute(condition);
Path<?> path = from.get(attribute);
Class<?> javaType = attribute.getType().getJavaType();
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);