Search in sources :

Example 31 with Type

use of com.mysema.codegen.model.Type 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)

Aggregations

Type (com.mysema.codegen.model.Type)31 SimpleType (com.mysema.codegen.model.SimpleType)11 EntityType (com.querydsl.codegen.EntityType)8 Test (org.junit.Test)8 ClassType (com.mysema.codegen.model.ClassType)6 PropertyType (com.querydsl.core.annotations.PropertyType)5 QueryType (com.querydsl.core.annotations.QueryType)5 Parameter (com.mysema.codegen.model.Parameter)4 TypeCategory (com.mysema.codegen.model.TypeCategory)4 Property (com.querydsl.codegen.Property)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3 DeclaredType (javax.lang.model.type.DeclaredType)3 NoType (javax.lang.model.type.NoType)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 MappingException (org.hibernate.MappingException)2 org.hibernate.mapping (org.hibernate.mapping)2 JavaWriter (com.mysema.codegen.JavaWriter)1 ScalaWriter (com.mysema.codegen.ScalaWriter)1 QueryDelegate (com.querydsl.core.annotations.QueryDelegate)1