Search in sources :

Example 1 with SqlResultSetMapping

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

the class AnnotationBinder method bindNamedJpaQueries.

private static void bindNamedJpaQueries(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
    QueryBinder.bindSqlResultSetMapping(annotatedElement.getAnnotation(SqlResultSetMapping.class), context, false);
    final SqlResultSetMappings ann = annotatedElement.getAnnotation(SqlResultSetMappings.class);
    if (ann != null) {
        for (SqlResultSetMapping current : ann.value()) {
            QueryBinder.bindSqlResultSetMapping(current, context, false);
        }
    }
    QueryBinder.bindQuery(annotatedElement.getAnnotation(NamedQuery.class), context, false);
    QueryBinder.bindQueries(annotatedElement.getAnnotation(NamedQueries.class), context, false);
    QueryBinder.bindNativeQuery(annotatedElement.getAnnotation(NamedNativeQuery.class), context, false);
    QueryBinder.bindNativeQueries(annotatedElement.getAnnotation(NamedNativeQueries.class), context, false);
}
Also used : NamedNativeQuery(jakarta.persistence.NamedNativeQuery) NamedNativeQueries(jakarta.persistence.NamedNativeQueries) SqlResultSetMappings(jakarta.persistence.SqlResultSetMappings) NamedQueries(jakarta.persistence.NamedQueries) NamedQuery(jakarta.persistence.NamedQuery) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping)

Example 2 with SqlResultSetMapping

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

the class JPAXMLOverriddenAnnotationReader method buildSqlResultsetMappings.

public static List<SqlResultSetMapping> buildSqlResultsetMappings(List<JaxbSqlResultSetMapping> elements, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    final List<SqlResultSetMapping> builtResultSetMappings = new ArrayList<>();
    // iterate over each <sql-result-set-mapping/> element
    for (JaxbSqlResultSetMapping resultSetMappingElement : elements) {
        final AnnotationDescriptor resultSetMappingAnnotation = new AnnotationDescriptor(SqlResultSetMapping.class);
        copyAttribute(resultSetMappingAnnotation, "name", resultSetMappingElement.getName(), 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 (JaxbEntityResult resultElement : resultSetMappingElement.getEntityResult()) {
            if (entityResultAnnotations == null) {
                entityResultAnnotations = new ArrayList<>();
            }
            // process the <entity-result/>
            entityResultAnnotations.add(buildEntityResult(resultElement, defaults, classLoaderAccess));
        }
        for (JaxbColumnResult resultElement : resultSetMappingElement.getColumnResult()) {
            if (columnResultAnnotations == null) {
                columnResultAnnotations = new ArrayList<>();
            }
            columnResultAnnotations.add(buildColumnResult(resultElement, defaults, classLoaderAccess));
        }
        for (JaxbConstructorResult resultElement : resultSetMappingElement.getConstructorResult()) {
            if (constructorResultAnnotations == null) {
                constructorResultAnnotations = new ArrayList<>();
            }
            constructorResultAnnotations.add(buildConstructorResult(resultElement, defaults, classLoaderAccess));
        }
        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()]));
        }
        builtResultSetMappings.add(AnnotationFactory.create(resultSetMappingAnnotation));
    }
    return builtResultSetMappings;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbColumnResult(org.hibernate.boot.jaxb.mapping.spi.JaxbColumnResult) JaxbSqlResultSetMapping(org.hibernate.boot.jaxb.mapping.spi.JaxbSqlResultSetMapping) ArrayList(java.util.ArrayList) EntityResult(jakarta.persistence.EntityResult) JaxbEntityResult(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityResult) ConstructorResult(jakarta.persistence.ConstructorResult) JaxbConstructorResult(org.hibernate.boot.jaxb.mapping.spi.JaxbConstructorResult) JaxbConstructorResult(org.hibernate.boot.jaxb.mapping.spi.JaxbConstructorResult) ColumnResult(jakarta.persistence.ColumnResult) JaxbColumnResult(org.hibernate.boot.jaxb.mapping.spi.JaxbColumnResult) JaxbEntityResult(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityResult) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) JaxbSqlResultSetMapping(org.hibernate.boot.jaxb.mapping.spi.JaxbSqlResultSetMapping)

Example 3 with SqlResultSetMapping

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

the class JPAXMLOverriddenAnnotationReader 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(jakarta.persistence.SqlResultSetMapping) JaxbSqlResultSetMapping(org.hibernate.boot.jaxb.mapping.spi.JaxbSqlResultSetMapping)

Example 4 with SqlResultSetMapping

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

the class JPAXMLOverriddenAnnotationReader method getSqlResultSetMappings.

private SqlResultSetMappings getSqlResultSetMappings(ManagedType root, XMLContext.Default defaults) {
    List<SqlResultSetMapping> results = root instanceof JaxbEntity ? buildSqlResultsetMappings(((JaxbEntity) root).getSqlResultSetMapping(), defaults, classLoaderAccess) : new ArrayList<>();
    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(jakarta.persistence.SqlResultSetMappings) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) JaxbSqlResultSetMapping(org.hibernate.boot.jaxb.mapping.spi.JaxbSqlResultSetMapping) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 5 with SqlResultSetMapping

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

the class JPAXMLOverriddenMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (!xmlMappingEnabled) {
        return Collections.emptyMap();
    } else {
        if (defaults == null) {
            defaults = new HashMap<>();
            XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema();
            defaults.put("schema", xmlDefaults.getSchema());
            defaults.put("catalog", xmlDefaults.getCatalog());
            defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
            defaults.put("cascade-persist", xmlDefaults.getCascadePersist());
            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 (JaxbEntityMappings entityMappings : xmlContext.getAllDocuments()) {
                List<JaxbSequenceGenerator> jaxbSequenceGenerators = entityMappings.getSequenceGenerator();
                List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
                if (sequenceGenerators == null) {
                    sequenceGenerators = new ArrayList<>();
                    defaults.put(SequenceGenerator.class, sequenceGenerators);
                }
                for (JaxbSequenceGenerator element : jaxbSequenceGenerators) {
                    sequenceGenerators.add(JPAXMLOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(element));
                }
                List<JaxbTableGenerator> jaxbTableGenerators = entityMappings.getTableGenerator();
                List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
                if (tableGenerators == null) {
                    tableGenerators = new ArrayList<>();
                    defaults.put(TableGenerator.class, tableGenerators);
                }
                for (JaxbTableGenerator element : jaxbTableGenerators) {
                    tableGenerators.add(JPAXMLOverriddenAnnotationReader.buildTableGeneratorAnnotation(element, xmlDefaults));
                }
                List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
                if (namedQueries == null) {
                    namedQueries = new ArrayList<>();
                    defaults.put(NamedQuery.class, namedQueries);
                }
                List<NamedQuery> currentNamedQueries = JPAXMLOverriddenAnnotationReader.buildNamedQueries(entityMappings.getNamedQuery(), xmlDefaults, classLoaderAccess);
                namedQueries.addAll(currentNamedQueries);
                List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
                if (namedNativeQueries == null) {
                    namedNativeQueries = new ArrayList<>();
                    defaults.put(NamedNativeQuery.class, namedNativeQueries);
                }
                List<NamedNativeQuery> currentNamedNativeQueries = JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(entityMappings.getNamedNativeQuery(), xmlDefaults, classLoaderAccess);
                namedNativeQueries.addAll(currentNamedNativeQueries);
                List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
                if (sqlResultSetMappings == null) {
                    sqlResultSetMappings = new ArrayList<>();
                    defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
                }
                List<SqlResultSetMapping> currentSqlResultSetMappings = JPAXMLOverriddenAnnotationReader.buildSqlResultsetMappings(entityMappings.getSqlResultSetMapping(), xmlDefaults, classLoaderAccess);
                sqlResultSetMappings.addAll(currentSqlResultSetMappings);
                List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
                if (namedStoredProcedureQueries == null) {
                    namedStoredProcedureQueries = new ArrayList<>();
                    defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
                }
                List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAXMLOverriddenAnnotationReader.buildNamedStoreProcedureQueries(entityMappings.getNamedStoredProcedureQuery(), xmlDefaults, classLoaderAccess);
                namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
            }
        }
        return defaults;
    }
}
Also used : JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGenerator) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) ArrayList(java.util.ArrayList) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbTableGenerator) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGenerator) SequenceGenerator(jakarta.persistence.SequenceGenerator) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbTableGenerator) TableGenerator(jakarta.persistence.TableGenerator) NamedQuery(jakarta.persistence.NamedQuery) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(jakarta.persistence.NamedStoredProcedureQuery)

Aggregations

SqlResultSetMapping (jakarta.persistence.SqlResultSetMapping)5 JaxbSqlResultSetMapping (org.hibernate.boot.jaxb.mapping.spi.JaxbSqlResultSetMapping)3 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)2 NamedQuery (jakarta.persistence.NamedQuery)2 SqlResultSetMappings (jakarta.persistence.SqlResultSetMappings)2 ArrayList (java.util.ArrayList)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 ColumnResult (jakarta.persistence.ColumnResult)1 ConstructorResult (jakarta.persistence.ConstructorResult)1 EntityResult (jakarta.persistence.EntityResult)1 NamedNativeQueries (jakarta.persistence.NamedNativeQueries)1 NamedQueries (jakarta.persistence.NamedQueries)1 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)1 SequenceGenerator (jakarta.persistence.SequenceGenerator)1 TableGenerator (jakarta.persistence.TableGenerator)1 List (java.util.List)1 JaxbColumnResult (org.hibernate.boot.jaxb.mapping.spi.JaxbColumnResult)1 JaxbConstructorResult (org.hibernate.boot.jaxb.mapping.spi.JaxbConstructorResult)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 JaxbEntityMappings (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings)1