Search in sources :

Example 6 with Property

use of com.querydsl.codegen.Property in project querydsl by querydsl.

the class HibernateDomainExporter method handleProperty.

private void handleProperty(EntityType entityType, Class<?> cl, org.hibernate.mapping.Property p) throws NoSuchMethodException, ClassNotFoundException {
    if (p.isBackRef()) {
        return;
    }
    Class<?> clazz = Object.class;
    try {
        clazz = p.getType().getReturnedClass();
    } catch (MappingException e) {
    // ignore
    }
    Type propertyType = getType(cl, clazz, p.getName());
    try {
        propertyType = getPropertyType(p, propertyType);
    } catch (MappingException e) {
    // ignore
    }
    AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
    propertyType = getTypeOverride(propertyType, annotated);
    if (propertyType == null) {
        return;
    }
    if (p.isComposite()) {
        EntityType embeddedType = createEmbeddableType(propertyType);
        Iterator<?> properties = ((Component) p.getValue()).getPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(embeddedType, embeddedType.getJavaClass(), (org.hibernate.mapping.Property) properties.next());
        }
        propertyType = embeddedType;
    } else if (propertyType.getCategory() == TypeCategory.ENTITY || p.getValue() instanceof ManyToOne) {
        propertyType = createEntityType(propertyType);
    } else if (propertyType.getCategory() == TypeCategory.CUSTOM) {
        propertyType = createEmbeddableType(propertyType);
    } else if (p.getValue() instanceof org.hibernate.mapping.Collection) {
        org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) p.getValue();
        if (collection.getElement() instanceof OneToMany) {
            String entityName = ((OneToMany) collection.getElement()).getReferencedEntityName();
            if (entityName != null) {
                if (collection.isMap()) {
                    Type keyType = typeFactory.get(Class.forName(propertyType.getParameters().get(0).getFullName()));
                    Type valueType = typeFactory.get(Class.forName(entityName));
                    propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
                } else {
                    Type componentType = typeFactory.get(Class.forName(entityName));
                    propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), componentType));
                }
            }
        } else if (collection.getElement() instanceof Component) {
            Component component = (Component) collection.getElement();
            Class<?> embeddedClass = Class.forName(component.getComponentClassName());
            EntityType embeddedType = createEmbeddableType(embeddedClass);
            Iterator<?> properties = component.getPropertyIterator();
            while (properties.hasNext()) {
                handleProperty(embeddedType, embeddedClass, (org.hibernate.mapping.Property) properties.next());
            }
        }
    }
    Property property = createProperty(entityType, p.getName(), propertyType, annotated);
    entityType.addProperty(property);
}
Also used : org.hibernate.mapping(org.hibernate.mapping) AnnotatedElement(java.lang.reflect.AnnotatedElement) MappingException(org.hibernate.MappingException) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Type(com.mysema.codegen.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Property(com.querydsl.codegen.Property)

Example 7 with Property

use of com.querydsl.codegen.Property in project querydsl by querydsl.

the class TypeElementHandler method toProperty.

private Property toProperty(EntityType entityType, String name, TypeMirror type, Annotations annotations) {
    // type
    Type propertyType = typeFactory.getType(type, true);
    if (annotations.isAnnotationPresent(QueryType.class)) {
        PropertyType propertyTypeAnn = annotations.getAnnotation(QueryType.class).value();
        if (propertyTypeAnn != PropertyType.NONE) {
            TypeCategory typeCategory = TypeCategory.valueOf(annotations.getAnnotation(QueryType.class).value().name());
            if (typeCategory == null) {
                return null;
            }
            propertyType = propertyType.as(typeCategory);
        } else {
            return null;
        }
    }
    // inits
    List<String> inits = Collections.emptyList();
    if (annotations.isAnnotationPresent(QueryInit.class)) {
        inits = Arrays.asList(annotations.getAnnotation(QueryInit.class).value());
    }
    return new Property(entityType, name, propertyType, inits);
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) EntityType(com.querydsl.codegen.EntityType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) QueryInit(com.querydsl.core.annotations.QueryInit) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) PropertyType(com.querydsl.core.annotations.PropertyType) QueryType(com.querydsl.core.annotations.QueryType) Property(com.querydsl.codegen.Property)

Example 8 with Property

use of com.querydsl.codegen.Property in project querydsl by querydsl.

the class ExtendedBeanSerializer method addToString.

@Override
protected void addToString(EntityType model, CodeWriter writer) throws IOException {
    Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
    if (primaryKeys == null || primaryKeys.isEmpty()) {
        super.addToString(model, writer);
        return;
    }
    StringBuilder toString = new StringBuilder();
    Map<String, Property> columnToProperty = new HashMap<String, Property>();
    for (Property property : model.getProperties()) {
        columnToProperty.put(property.getAnnotation(Column.class).value(), property);
    }
    for (PrimaryKeyData pk : primaryKeys) {
        for (String column : pk.getColumns()) {
            Property property = columnToProperty.get(column);
            String propName = property.getEscapedName();
            if (toString.length() > 0) {
                toString.append("+ \";\" + ");
            } else {
                toString.append("\"" + model.getSimpleName() + "#\" + ");
            }
            toString.append(propName);
        }
    }
    // toString
    writer.annotation(Override.class);
    writer.beginPublicMethod(Types.STRING, "toString");
    // writer.line("if (", anyColumnIsNull + ") {");
    // writer.line("    return super.toString();");
    // writer.line("}");
    writer.line("return ", toString + ";");
    writer.end();
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) HashMap(java.util.HashMap) Collection(java.util.Collection) Property(com.querydsl.codegen.Property)

Example 9 with Property

use of com.querydsl.codegen.Property in project querydsl by querydsl.

the class ExtendedBeanSerializer method bodyEnd.

@SuppressWarnings("unchecked")
@Override
protected void bodyEnd(EntityType model, CodeWriter writer) throws IOException {
    Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
    if (primaryKeys == null || primaryKeys.isEmpty()) {
        return;
    }
    Map<String, Property> columnToProperty = new HashMap<String, Property>();
    for (Property property : model.getProperties()) {
        columnToProperty.put(property.getAnnotation(Column.class).value(), property);
    }
    StringBuilder anyColumnIsNull = new StringBuilder();
    StringBuilder columnEquals = new StringBuilder();
    List<String> properties = new ArrayList<String>();
    for (PrimaryKeyData pk : primaryKeys) {
        for (String column : pk.getColumns()) {
            Property property = columnToProperty.get(column);
            String propName = property.getEscapedName();
            if (anyColumnIsNull.length() > 0) {
                anyColumnIsNull.append(" || ");
                columnEquals.append(" && ");
            }
            anyColumnIsNull.append(propName).append(" == null");
            columnEquals.append(propName).append(".equals(obj.").append(propName).append(")");
            properties.add(propName);
        }
    }
    // equals
    writer.annotation(Override.class);
    writer.beginPublicMethod(Types.BOOLEAN_P, "equals", o);
    writer.line("if (", anyColumnIsNull + ") {");
    writer.line("    return super.equals(o);");
    writer.line("}");
    writer.line("if (!(o instanceof ", model.getSimpleName(), ")) {");
    writer.line("    return false;");
    writer.line("}");
    writer.line(model.getSimpleName(), " obj = (", model.getSimpleName(), ") o;");
    writer.line("return ", columnEquals + ";");
    writer.end();
    // hashCode
    writer.annotation(Override.class);
    writer.beginPublicMethod(Types.INT, "hashCode");
    writer.line("if (", anyColumnIsNull + ") {");
    writer.line("    return super.hashCode();");
    writer.line("}");
    writer.line("final int prime = 31;");
    writer.line("int result = 1;");
    for (String property : properties) {
        writer.line("result = prime * result + ", property, ".hashCode();");
    }
    writer.line("return result;");
    writer.end();
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Property(com.querydsl.codegen.Property)

Example 10 with Property

use of com.querydsl.codegen.Property in project querydsl by querydsl.

the class OrdinalPositionComparator method compare.

@Override
public int compare(Property property1, Property property2) {
    Integer comparison = null;
    for (Property property : Arrays.asList(property1, property2)) {
        Map<Object, Object> data = property.getData();
        ColumnMetadata columnMetadata = (ColumnMetadata) data.get("COLUMN");
        int index = columnMetadata.getIndex();
        comparison = comparison == null ? index : comparison - index;
    }
    return comparison;
}
Also used : ColumnMetadata(com.querydsl.sql.ColumnMetadata) Property(com.querydsl.codegen.Property)

Aggregations

Property (com.querydsl.codegen.Property)11 EntityType (com.querydsl.codegen.EntityType)6 Type (com.querydsl.codegen.utils.model.Type)3 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)3 AnnotatedElement (java.lang.reflect.AnnotatedElement)3 HashMap (java.util.HashMap)3 SimpleType (com.mysema.codegen.model.SimpleType)2 Type (com.mysema.codegen.model.Type)2 ClassType (com.querydsl.codegen.utils.model.ClassType)2 SimpleType (com.querydsl.codegen.utils.model.SimpleType)2 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)2 ColumnImpl (com.querydsl.sql.ColumnImpl)2 ColumnMetadata (com.querydsl.sql.ColumnMetadata)2 Collection (java.util.Collection)2 MappingException (org.hibernate.MappingException)2 org.hibernate.mapping (org.hibernate.mapping)2 Test (org.junit.Test)2 JavaWriter (com.querydsl.codegen.utils.JavaWriter)1 SimpleCompiler (com.querydsl.codegen.utils.SimpleCompiler)1 PropertyType (com.querydsl.core.annotations.PropertyType)1