Search in sources :

Example 6 with NamedNativeQuery

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

the class JPAXMLOverriddenAnnotationReader method buildNamedNativeQueries.

public static List<NamedNativeQuery> buildNamedNativeQueries(List<JaxbNamedNativeQuery> elements, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    List<NamedNativeQuery> namedQueries = new ArrayList<>();
    for (JaxbNamedNativeQuery element : elements) {
        AnnotationDescriptor ann = new AnnotationDescriptor(NamedNativeQuery.class);
        copyAttribute(ann, "name", element.getName(), false);
        copyAttribute(ann, "query", element.getQuery(), true);
        buildQueryHints(element.getHint(), ann);
        String clazzName = element.getResultClass();
        if (StringHelper.isNotEmpty(clazzName)) {
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            ann.setValue("resultClass", clazz);
        }
        copyAttribute(ann, "result-set-mapping", element.getResultSetMapping(), false);
        namedQueries.add(AnnotationFactory.create(ann));
    }
    return namedQueries;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbNamedNativeQuery(org.hibernate.boot.jaxb.mapping.JaxbNamedNativeQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) JaxbNamedNativeQuery(org.hibernate.boot.jaxb.mapping.JaxbNamedNativeQuery) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException) IdClass(jakarta.persistence.IdClass) MapKeyClass(jakarta.persistence.MapKeyClass) JaxbMapKeyClass(org.hibernate.boot.jaxb.mapping.JaxbMapKeyClass) JaxbIdClass(org.hibernate.boot.jaxb.mapping.JaxbIdClass)

Example 7 with NamedNativeQuery

use of jakarta.persistence.NamedNativeQuery 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.getNamedQueries(), 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.getNamedNativeQueries(), 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.getSqlResultSetMappings(), 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.getNamedProcedureQueries(), xmlDefaults, classLoaderAccess);
                namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
            }
        }
        return defaults;
    }
}
Also used : JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.JaxbSequenceGenerator) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) ArrayList(java.util.ArrayList) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.JaxbEntityMappings) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.JaxbTableGenerator) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.JaxbSequenceGenerator) SequenceGenerator(jakarta.persistence.SequenceGenerator) TableGenerator(jakarta.persistence.TableGenerator) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.JaxbTableGenerator) NamedQuery(jakarta.persistence.NamedQuery) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(jakarta.persistence.NamedStoredProcedureQuery)

Example 8 with NamedNativeQuery

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

the class NamedQueryTest method testNativeNamedQueriesOrdinalParametersConflict.

@Test
public void testNativeNamedQueriesOrdinalParametersConflict() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedNativeQuery");
        query.setParameter(1, GAME_TITLES[0]);
        List list = query.getResultList();
        assertEquals(1, list.size());
        final Session session = entityManager.unwrap(Session.class);
        final org.hibernate.query.Query sessionQuery = session.createNativeQuery("select * from Game g where title = ?");
        sessionQuery.setParameter(1, GAME_TITLES[0]);
        list = sessionQuery.getResultList();
        query.setParameter(1, GAME_TITLES[0]);
        assertEquals(1, list.size());
    });
}
Also used : Query(jakarta.persistence.Query) NativeQuery(org.hibernate.query.NativeQuery) NamedQuery(jakarta.persistence.NamedQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with NamedNativeQuery

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

the class NamedQueryTest method testNativeNamedQueriesOrdinalParametersConflict2.

@Test
public void testNativeNamedQueriesOrdinalParametersConflict2() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedNativeQuery");
        query.setParameter(1, GAME_TITLES[0]);
        List list = query.getResultList();
        assertEquals(1, list.size());
        final Session session = entityManager.unwrap(Session.class);
        final org.hibernate.query.Query sessionQuery = session.getNamedNativeQuery("NamedNativeQuery");
        sessionQuery.setParameter(1, GAME_TITLES[0]);
        list = sessionQuery.getResultList();
        query.setParameter(1, GAME_TITLES[0]);
        assertEquals(1, list.size());
    });
}
Also used : Query(jakarta.persistence.Query) NativeQuery(org.hibernate.query.NativeQuery) NamedQuery(jakarta.persistence.NamedQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

NamedNativeQuery (jakarta.persistence.NamedNativeQuery)9 NamedQuery (jakarta.persistence.NamedQuery)6 Query (jakarta.persistence.Query)5 List (java.util.List)5 NativeQuery (org.hibernate.query.NativeQuery)4 Test (org.junit.Test)4 JaxbNamedNativeQuery (org.hibernate.boot.jaxb.mapping.JaxbNamedNativeQuery)3 ArrayList (java.util.ArrayList)2 Session (org.hibernate.Session)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)2 IdClass (jakarta.persistence.IdClass)1 MapKeyClass (jakarta.persistence.MapKeyClass)1 NamedNativeQueries (jakarta.persistence.NamedNativeQueries)1 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)1 SequenceGenerator (jakarta.persistence.SequenceGenerator)1 SqlResultSetMapping (jakarta.persistence.SqlResultSetMapping)1 TableGenerator (jakarta.persistence.TableGenerator)1 AnnotationException (org.hibernate.AnnotationException)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.JaxbEntity)1