Search in sources :

Example 6 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class CollQuerySerializer method visit.

@Override
public Void visit(Path<?> path, Void context) {
    final PathType pathType = path.getMetadata().getPathType();
    if (pathType == PathType.PROPERTY) {
        final Path<?> parent = path.getMetadata().getParent();
        final String property = path.getMetadata().getName();
        final Class<?> parentType = parent.getType();
        try {
            // getter
            Method m = getAccessor(parentType, property);
            if (m != null && Modifier.isPublic(m.getModifiers())) {
                handle(parent);
                append(".").append(m.getName()).append("()");
            } else {
                // field
                Field f = getField(parentType, property);
                if (f != null && Modifier.isPublic(f.getModifiers())) {
                    handle(parent);
                    append(".").append(property);
                } else {
                    // field access by reflection
                    append(CollQueryFunctions.class.getName() + ".<");
                    append((path.getType()).getName()).append(">get(");
                    handle(parent);
                    append(", \"" + property + "\")");
                }
            }
        } catch (Exception e) {
            throw new QueryException(e);
        }
    } else if (pathType == PathType.DELEGATE) {
        append("(");
        append("(").append(path.getType().getName()).append(")");
        path.getMetadata().getParent().accept(this, context);
        append(")");
    } else {
        List<Object> args = new ArrayList<Object>(2);
        if (path.getMetadata().getParent() != null) {
            args.add(path.getMetadata().getParent());
        }
        args.add(path.getMetadata().getElement());
        final Template template = getTemplate(pathType);
        for (Template.Element element : template.getElements()) {
            Object rv = element.convert(args);
            if (rv instanceof Expression) {
                ((Expression<?>) rv).accept(this, context);
            } else if (element.isString()) {
                append(rv.toString());
            } else {
                visitConstant(rv);
            }
        }
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) QueryException(com.querydsl.core.QueryException) IntrospectionException(java.beans.IntrospectionException) Field(java.lang.reflect.Field) QueryException(com.querydsl.core.QueryException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class AnnotationMapper method createMap.

@Override
public Map<Path<?>, Object> createMap(RelationalPath<?> path, Object object) {
    try {
        Map<String, Path<?>> columnToPath = new HashMap<String, Path<?>>();
        for (Path<?> column : path.getColumns()) {
            columnToPath.put(ColumnMetadata.getName(column), column);
        }
        Map<Path<?>, Object> values = new HashMap<Path<?>, Object>();
        for (Field field : ReflectionUtils.getFields(object.getClass())) {
            Column ann = field.getAnnotation(Column.class);
            if (ann != null) {
                field.setAccessible(true);
                Object propertyValue = field.get(object);
                if (propertyValue != null) {
                    if (columnToPath.containsKey(ann.value())) {
                        values.put(columnToPath.get(ann.value()), propertyValue);
                    }
                } else if (withNullBindings) {
                    values.put(columnToPath.get(ann.value()), Null.DEFAULT);
                }
            }
        }
        return values;
    } catch (IllegalAccessException e) {
        throw new QueryException(e);
    }
}
Also used : RelationalPath(com.querydsl.sql.RelationalPath) Path(com.querydsl.core.types.Path) Field(java.lang.reflect.Field) QueryException(com.querydsl.core.QueryException) HashMap(java.util.HashMap) Column(com.querydsl.sql.Column)

Example 8 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class DefaultMapper method createMap.

@Override
public Map<Path<?>, Object> createMap(RelationalPath<?> entity, Object bean) {
    try {
        Map<Path<?>, Object> values = Maps.newLinkedHashMap();
        Class<?> beanClass = bean.getClass();
        Map<String, Path<?>> columns = getColumns(entity);
        // populate in column order
        for (Map.Entry<String, Path<?>> entry : columns.entrySet()) {
            Path<?> path = entry.getValue();
            Field beanField = ReflectionUtils.getFieldOrNull(beanClass, entry.getKey());
            if (beanField != null && !Modifier.isStatic(beanField.getModifiers())) {
                beanField.setAccessible(true);
                Object propertyValue = beanField.get(bean);
                if (propertyValue != null) {
                    values.put(path, propertyValue);
                } else if (withNullBindings && !isPrimaryKeyColumn(entity, path)) {
                    values.put(path, Null.DEFAULT);
                }
            }
        }
        return values;
    } catch (IllegalAccessException e) {
        throw new QueryException(e);
    }
}
Also used : RelationalPath(com.querydsl.sql.RelationalPath) Path(com.querydsl.core.types.Path) Field(java.lang.reflect.Field) QueryException(com.querydsl.core.QueryException) Map(java.util.Map)

Example 9 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class JavaSE7SQLExceptionWrapper method wrap.

@Override
public RuntimeException wrap(String message, SQLException exception) {
    QueryException rv = new QueryException(message, exception);
    SQLException linkedException = exception.getNextException();
    while (linkedException != null) {
        rv.addSuppressed(linkedException);
        linkedException = linkedException.getNextException();
    }
    return rv;
}
Also used : QueryException(com.querydsl.core.QueryException) SQLException(java.sql.SQLException)

Example 10 with QueryException

use of com.querydsl.core.QueryException in project querydsl by querydsl.

the class AbstractDomainExporter method execute.

/**
     * Export the contents
     *
     * @throws IOException
     */
public void execute() throws IOException {
    // collect types
    try {
        collectTypes();
    } catch (Exception e) {
        throw new QueryException(e);
    }
    // go through supertypes
    Set<Supertype> additions = Sets.newHashSet();
    for (Map.Entry<Class<?>, EntityType> entry : allTypes.entrySet()) {
        EntityType entityType = entry.getValue();
        if (entityType.getSuperType() != null && !allTypes.containsKey(entityType.getSuperType().getType().getJavaClass())) {
            additions.add(entityType.getSuperType());
        }
    }
    for (Supertype type : additions) {
        type.setEntityType(createEntityType(type.getType(), this.superTypes));
    }
    // merge supertype fields into subtypes
    Set<EntityType> handled = new HashSet<EntityType>();
    for (EntityType type : superTypes.values()) {
        addSupertypeFields(type, allTypes, handled);
    }
    for (EntityType type : entityTypes.values()) {
        addSupertypeFields(type, allTypes, handled);
    }
    for (EntityType type : embeddableTypes.values()) {
        addSupertypeFields(type, allTypes, handled);
    }
    // serialize them
    serialize(superTypes, supertypeSerializer);
    serialize(embeddableTypes, embeddableSerializer);
    serialize(entityTypes, entitySerializer);
}
Also used : QueryException(com.querydsl.core.QueryException) QueryException(com.querydsl.core.QueryException)

Aggregations

QueryException (com.querydsl.core.QueryException)12 SQLException (java.sql.SQLException)5 Field (java.lang.reflect.Field)3 Path (com.querydsl.core.types.Path)2 RelationalPath (com.querydsl.sql.RelationalPath)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 Column (com.querydsl.sql.Column)1 IntrospectionException (java.beans.IntrospectionException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Query (org.hibernate.Query)1 Test (org.junit.Test)1