Search in sources :

Example 61 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildNamedStoreProcedureQueries.

public static List<NamedStoredProcedureQuery> buildNamedStoreProcedureQueries(Element element, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    if (element == null) {
        return new ArrayList<>();
    }
    List namedStoredProcedureElements = element.elements("named-stored-procedure-query");
    List<NamedStoredProcedureQuery> namedStoredProcedureQueries = new ArrayList<>();
    for (Object obj : namedStoredProcedureElements) {
        Element subElement = (Element) obj;
        AnnotationDescriptor ann = new AnnotationDescriptor(NamedStoredProcedureQuery.class);
        copyStringAttribute(ann, subElement, "name", true);
        copyStringAttribute(ann, subElement, "procedure-name", true);
        List<Element> elements = subElement.elements("parameter");
        List<StoredProcedureParameter> storedProcedureParameters = new ArrayList<>();
        for (Element parameterElement : elements) {
            AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor(StoredProcedureParameter.class);
            copyStringAttribute(parameterDescriptor, parameterElement, "name", false);
            String modeValue = parameterElement.attributeValue("mode");
            if (modeValue == null) {
                parameterDescriptor.setValue("mode", ParameterMode.IN);
            } else {
                parameterDescriptor.setValue("mode", ParameterMode.valueOf(modeValue.toUpperCase(Locale.ROOT)));
            }
            String clazzName = parameterElement.attributeValue("class");
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            parameterDescriptor.setValue("type", clazz);
            storedProcedureParameters.add(AnnotationFactory.create(parameterDescriptor));
        }
        ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
        elements = subElement.elements("result-class");
        List<Class> returnClasses = new ArrayList<>();
        for (Element classElement : elements) {
            String clazzName = classElement.getTextTrim();
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            returnClasses.add(clazz);
        }
        ann.setValue("resultClasses", returnClasses.toArray(new Class[returnClasses.size()]));
        elements = subElement.elements("result-set-mapping");
        List<String> resultSetMappings = new ArrayList<>();
        for (Element resultSetMappingElement : elements) {
            resultSetMappings.add(resultSetMappingElement.getTextTrim());
        }
        ann.setValue("resultSetMappings", resultSetMappings.toArray(new String[resultSetMappings.size()]));
        elements = subElement.elements("hint");
        buildQueryHints(elements, ann);
        namedStoredProcedureQueries.add(AnnotationFactory.create(ann));
    }
    return namedStoredProcedureQueries;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StoredProcedureParameter(javax.persistence.StoredProcedureParameter) AnnotationException(org.hibernate.AnnotationException) ArrayList(java.util.ArrayList) List(java.util.List) AccessibleObject(java.lang.reflect.AccessibleObject) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Example 62 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getEntityListeners.

private EntityListeners getEntityListeners(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("entity-listeners") : null;
    if (element != null) {
        List<Class> entityListenerClasses = new ArrayList<>();
        for (Element subelement : (List<Element>) element.elements("entity-listener")) {
            String className = subelement.attributeValue("class");
            try {
                entityListenerClasses.add(classLoaderAccess.classForName(XMLContext.buildSafeClassName(className, defaults)));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find " + element.getPath() + ".class: " + className, e);
            }
        }
        AnnotationDescriptor ad = new AnnotationDescriptor(EntityListeners.class);
        ad.setValue("value", entityListenerClasses.toArray(new Class[entityListenerClasses.size()]));
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(EntityListeners.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) ArrayList(java.util.ArrayList) List(java.util.List) EntityListeners(javax.persistence.EntityListeners)

Example 63 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildSqlResultsetMappings.

public static List<SqlResultSetMapping> buildSqlResultsetMappings(Element element, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    final List<SqlResultSetMapping> builtResultSetMappings = new ArrayList<>();
    if (element == null) {
        return builtResultSetMappings;
    }
    // iterate over each <sql-result-set-mapping/> element
    for (Object resultSetMappingElementObject : element.elements("sql-result-set-mapping")) {
        final Element resultSetMappingElement = (Element) resultSetMappingElementObject;
        final AnnotationDescriptor resultSetMappingAnnotation = new AnnotationDescriptor(SqlResultSetMapping.class);
        copyStringAttribute(resultSetMappingAnnotation, resultSetMappingElement, "name", true);
        // iterate over the <sql-result-set-mapping/> sub-elements, which should include:
        // * <entity-result/>
        // * <column-result/>
        // * <constructor-result/>
        List<EntityResult> entityResultAnnotations = null;
        List<ColumnResult> columnResultAnnotations = null;
        List<ConstructorResult> constructorResultAnnotations = null;
        for (Object resultElementObject : resultSetMappingElement.elements()) {
            final Element resultElement = (Element) resultElementObject;
            if ("entity-result".equals(resultElement.getName())) {
                if (entityResultAnnotations == null) {
                    entityResultAnnotations = new ArrayList<>();
                }
                // process the <entity-result/>
                entityResultAnnotations.add(buildEntityResult(resultElement, defaults, classLoaderAccess));
            } else if ("column-result".equals(resultElement.getName())) {
                if (columnResultAnnotations == null) {
                    columnResultAnnotations = new ArrayList<>();
                }
                columnResultAnnotations.add(buildColumnResult(resultElement, defaults, classLoaderAccess));
            } else if ("constructor-result".equals(resultElement.getName())) {
                if (constructorResultAnnotations == null) {
                    constructorResultAnnotations = new ArrayList<>();
                }
                constructorResultAnnotations.add(buildConstructorResult(resultElement, defaults, classLoaderAccess));
            } else {
                // most likely the <result-class/> this code used to handle.  I have left the code here,
                // but commented it out for now.  I'll just log a warning for now.
                LOG.debug("Encountered unrecognized sql-result-set-mapping sub-element : " + resultElement.getName());
            // String clazzName = subelement.attributeValue( "result-class" );
            // if ( StringHelper.isNotEmpty( clazzName ) ) {
            // Class clazz;
            // try {
            // clazz = ReflectHelper.classForName(
            // XMLContext.buildSafeClassName( clazzName, defaults ),
            // JPAOverriddenAnnotationReader.class
            // );
            // }
            // catch ( ClassNotFoundException e ) {
            // throw new AnnotationException( "Unable to find entity-class: " + clazzName, e );
            // }
            // ann.setValue( "resultClass", clazz );
            // }
            }
        }
        if (entityResultAnnotations != null && !entityResultAnnotations.isEmpty()) {
            resultSetMappingAnnotation.setValue("entities", entityResultAnnotations.toArray(new EntityResult[entityResultAnnotations.size()]));
        }
        if (columnResultAnnotations != null && !columnResultAnnotations.isEmpty()) {
            resultSetMappingAnnotation.setValue("columns", columnResultAnnotations.toArray(new ColumnResult[columnResultAnnotations.size()]));
        }
        if (constructorResultAnnotations != null && !constructorResultAnnotations.isEmpty()) {
            resultSetMappingAnnotation.setValue("classes", constructorResultAnnotations.toArray(new ConstructorResult[constructorResultAnnotations.size()]));
        }
        // this was part of the old code too, but could never figure out what it is supposed to do...
        // copyStringAttribute( ann, subelement, "result-set-mapping", false );
        builtResultSetMappings.add(AnnotationFactory.create(resultSetMappingAnnotation));
    }
    return builtResultSetMappings;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) EntityResult(javax.persistence.EntityResult) ConstructorResult(javax.persistence.ConstructorResult) ColumnResult(javax.persistence.ColumnResult) AccessibleObject(java.lang.reflect.AccessibleObject) SqlResultSetMapping(javax.persistence.SqlResultSetMapping)

Example 64 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getPrimaryKeyJoinColumns.

/**
 * @param mergeWithAnnotations Whether to use Java annotations for this
 * element, if present and not disabled by the XMLContext defaults.
 * In some contexts (such as an association mapping) merging with
 * annotations is never allowed.
 */
private PrimaryKeyJoinColumns getPrimaryKeyJoinColumns(Element element, XMLContext.Default defaults, boolean mergeWithAnnotations) {
    PrimaryKeyJoinColumn[] columns = buildPrimaryKeyJoinColumns(element);
    if (mergeWithAnnotations) {
        if (columns.length == 0 && defaults.canUseJavaAnnotations()) {
            PrimaryKeyJoinColumn annotation = getPhysicalAnnotation(PrimaryKeyJoinColumn.class);
            if (annotation != null) {
                columns = new PrimaryKeyJoinColumn[] { annotation };
            } else {
                PrimaryKeyJoinColumns annotations = getPhysicalAnnotation(PrimaryKeyJoinColumns.class);
                columns = annotations != null ? annotations.value() : columns;
            }
        }
    }
    if (columns.length > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(PrimaryKeyJoinColumns.class);
        ad.setValue("value", columns);
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns)

Example 65 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getNamedQueries.

private NamedQueries getNamedQueries(Element tree, XMLContext.Default defaults) {
    // TODO avoid the Proxy Creation (@NamedQueries) when possible
    List<NamedQuery> queries = (List<NamedQuery>) buildNamedQueries(tree, false, defaults, classLoaderAccess);
    if (defaults.canUseJavaAnnotations()) {
        NamedQuery annotation = getPhysicalAnnotation(NamedQuery.class);
        addNamedQueryIfNeeded(annotation, queries);
        NamedQueries annotations = getPhysicalAnnotation(NamedQueries.class);
        if (annotations != null) {
            for (NamedQuery current : annotations.value()) {
                addNamedQueryIfNeeded(current, queries);
            }
        }
    }
    if (queries.size() > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(NamedQueries.class);
        ad.setValue("value", queries.toArray(new NamedQuery[queries.size()]));
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) NamedQueries(javax.persistence.NamedQueries) NamedQuery(javax.persistence.NamedQuery)

Aggregations

AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)70 AnnotatedElement (java.lang.reflect.AnnotatedElement)46 Element (org.dom4j.Element)46 ArrayList (java.util.ArrayList)23 AnnotationException (org.hibernate.AnnotationException)15 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)13 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)13 List (java.util.List)11 JoinColumn (javax.persistence.JoinColumn)11 MapKeyClass (javax.persistence.MapKeyClass)10 IdClass (javax.persistence.IdClass)9 Annotation (java.lang.annotation.Annotation)8 AttributeOverride (javax.persistence.AttributeOverride)8 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)8 AssociationOverride (javax.persistence.AssociationOverride)7 Column (javax.persistence.Column)7 MapKeyColumn (javax.persistence.MapKeyColumn)7 OrderColumn (javax.persistence.OrderColumn)7 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)7 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)7