Search in sources :

Example 1 with RelationalPath

use of com.querydsl.sql.RelationalPath 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 2 with RelationalPath

use of com.querydsl.sql.RelationalPath 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 = new LinkedHashMap<>();
        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) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) QueryException(com.querydsl.core.QueryException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

QueryException (com.querydsl.core.QueryException)2 Path (com.querydsl.core.types.Path)2 RelationalPath (com.querydsl.sql.RelationalPath)2 Field (java.lang.reflect.Field)2 Column (com.querydsl.sql.Column)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1