Search in sources :

Example 1 with NamedStoredProcedureQuery

use of javax.persistence.NamedStoredProcedureQuery in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getNamedStoredProcedureQueries.

private NamedStoredProcedureQueries getNamedStoredProcedureQueries(Element tree, XMLContext.Default defaults) {
    List<NamedStoredProcedureQuery> queries = buildNamedStoreProcedureQueries(tree, defaults, classLoaderAccess);
    if (defaults.canUseJavaAnnotations()) {
        NamedStoredProcedureQuery annotation = getPhysicalAnnotation(NamedStoredProcedureQuery.class);
        addNamedStoredProcedureQueryIfNeeded(annotation, queries);
        NamedStoredProcedureQueries annotations = getPhysicalAnnotation(NamedStoredProcedureQueries.class);
        if (annotations != null) {
            for (NamedStoredProcedureQuery current : annotations.value()) {
                addNamedStoredProcedureQueryIfNeeded(current, queries);
            }
        }
    }
    if (queries.size() > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(NamedStoredProcedureQueries.class);
        ad.setValue("value", queries.toArray(new NamedStoredProcedureQuery[queries.size()]));
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) NamedStoredProcedureQueries(javax.persistence.NamedStoredProcedureQueries) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Example 2 with NamedStoredProcedureQuery

use of javax.persistence.NamedStoredProcedureQuery in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method addNamedStoredProcedureQueryIfNeeded.

private void addNamedStoredProcedureQueryIfNeeded(NamedStoredProcedureQuery annotation, List<NamedStoredProcedureQuery> queries) {
    if (annotation != null) {
        String queryName = annotation.name();
        boolean present = false;
        for (NamedStoredProcedureQuery current : queries) {
            if (current.name().equals(queryName)) {
                present = true;
                break;
            }
        }
        if (!present) {
            queries.add(annotation);
        }
    }
}
Also used : NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Example 3 with NamedStoredProcedureQuery

use of javax.persistence.NamedStoredProcedureQuery 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<NamedStoredProcedureQuery>();
    }
    List namedStoredProcedureElements = element.elements("named-stored-procedure-query");
    List<NamedStoredProcedureQuery> namedStoredProcedureQueries = new ArrayList<NamedStoredProcedureQuery>();
    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<StoredProcedureParameter>();
        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((StoredProcedureParameter) AnnotationFactory.create(parameterDescriptor));
        }
        ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
        elements = subElement.elements("result-class");
        List<Class> returnClasses = new ArrayList<Class>();
        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<String>();
        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((NamedStoredProcedureQuery) 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 4 with NamedStoredProcedureQuery

use of javax.persistence.NamedStoredProcedureQuery in project hibernate-orm by hibernate.

the class JPAMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (defaults == null) {
        defaults = new HashMap<Object, Object>();
        XMLContext.Default xmlDefaults = xmlContext.getDefault(null);
        defaults.put("schema", xmlDefaults.getSchema());
        defaults.put("catalog", xmlDefaults.getCatalog());
        defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
        List<Class> entityListeners = new ArrayList<Class>();
        for (String className : xmlContext.getDefaultEntityListeners()) {
            try {
                entityListeners.add(classLoaderAccess.classForName(className));
            } catch (ClassLoadingException e) {
                throw new IllegalStateException("Default entity listener class not found: " + className);
            }
        }
        defaults.put(EntityListeners.class, entityListeners);
        for (Element element : xmlContext.getAllDocuments()) {
            @SuppressWarnings("unchecked") List<Element> elements = element.elements("sequence-generator");
            List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
            if (sequenceGenerators == null) {
                sequenceGenerators = new ArrayList<SequenceGenerator>();
                defaults.put(SequenceGenerator.class, sequenceGenerators);
            }
            for (Element subelement : elements) {
                sequenceGenerators.add(JPAOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(subelement));
            }
            elements = element.elements("table-generator");
            List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
            if (tableGenerators == null) {
                tableGenerators = new ArrayList<TableGenerator>();
                defaults.put(TableGenerator.class, tableGenerators);
            }
            for (Element subelement : elements) {
                tableGenerators.add(JPAOverriddenAnnotationReader.buildTableGeneratorAnnotation(subelement, xmlDefaults));
            }
            List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
            if (namedQueries == null) {
                namedQueries = new ArrayList<NamedQuery>();
                defaults.put(NamedQuery.class, namedQueries);
            }
            List<NamedQuery> currentNamedQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, false, xmlDefaults, classLoaderAccess);
            namedQueries.addAll(currentNamedQueries);
            List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
            if (namedNativeQueries == null) {
                namedNativeQueries = new ArrayList<NamedNativeQuery>();
                defaults.put(NamedNativeQuery.class, namedNativeQueries);
            }
            List<NamedNativeQuery> currentNamedNativeQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, true, xmlDefaults, classLoaderAccess);
            namedNativeQueries.addAll(currentNamedNativeQueries);
            List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
            if (sqlResultSetMappings == null) {
                sqlResultSetMappings = new ArrayList<SqlResultSetMapping>();
                defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
            }
            List<SqlResultSetMapping> currentSqlResultSetMappings = JPAOverriddenAnnotationReader.buildSqlResultsetMappings(element, xmlDefaults, classLoaderAccess);
            sqlResultSetMappings.addAll(currentSqlResultSetMappings);
            List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
            if (namedStoredProcedureQueries == null) {
                namedStoredProcedureQueries = new ArrayList<NamedStoredProcedureQuery>();
                defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
            }
            List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAOverriddenAnnotationReader.buildNamedStoreProcedureQueries(element, xmlDefaults, classLoaderAccess);
            namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
        }
    }
    return defaults;
}
Also used : NamedNativeQuery(javax.persistence.NamedNativeQuery) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) SequenceGenerator(javax.persistence.SequenceGenerator) TableGenerator(javax.persistence.TableGenerator) NamedQuery(javax.persistence.NamedQuery) SqlResultSetMapping(javax.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Aggregations

NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Element (org.dom4j.Element)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 IdClass (javax.persistence.IdClass)1 MapKeyClass (javax.persistence.MapKeyClass)1 NamedNativeQuery (javax.persistence.NamedNativeQuery)1 NamedQuery (javax.persistence.NamedQuery)1 NamedStoredProcedureQueries (javax.persistence.NamedStoredProcedureQueries)1 SequenceGenerator (javax.persistence.SequenceGenerator)1 SqlResultSetMapping (javax.persistence.SqlResultSetMapping)1 StoredProcedureParameter (javax.persistence.StoredProcedureParameter)1 TableGenerator (javax.persistence.TableGenerator)1 AnnotationException (org.hibernate.AnnotationException)1