Search in sources :

Example 1 with SqlResultSetMapping

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

the class JPAOverriddenAnnotationReader method addSqlResultsetMappingIfNeeded.

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

Example 2 with SqlResultSetMapping

use of javax.persistence.SqlResultSetMapping 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)

Example 3 with SqlResultSetMapping

use of javax.persistence.SqlResultSetMapping 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<SqlResultSetMapping>();
    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<EntityResult>();
                }
                // process the <entity-result/>
                entityResultAnnotations.add(buildEntityResult(resultElement, defaults, classLoaderAccess));
            } else if ("column-result".equals(resultElement.getName())) {
                if (columnResultAnnotations == null) {
                    columnResultAnnotations = new ArrayList<ColumnResult>();
                }
                columnResultAnnotations.add(buildColumnResult(resultElement, defaults, classLoaderAccess));
            } else if ("constructor-result".equals(resultElement.getName())) {
                if (constructorResultAnnotations == null) {
                    constructorResultAnnotations = new ArrayList<ConstructorResult>();
                }
                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((SqlResultSetMapping) 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 4 with SqlResultSetMapping

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

the class JPAOverriddenAnnotationReader method getSqlResultSetMappings.

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

Aggregations

SqlResultSetMapping (javax.persistence.SqlResultSetMapping)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 ArrayList (java.util.ArrayList)2 Element (org.dom4j.Element)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 List (java.util.List)1 ColumnResult (javax.persistence.ColumnResult)1 ConstructorResult (javax.persistence.ConstructorResult)1 EntityResult (javax.persistence.EntityResult)1 NamedNativeQuery (javax.persistence.NamedNativeQuery)1 NamedQuery (javax.persistence.NamedQuery)1 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)1 SequenceGenerator (javax.persistence.SequenceGenerator)1 SqlResultSetMappings (javax.persistence.SqlResultSetMappings)1 TableGenerator (javax.persistence.TableGenerator)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1