Search in sources :

Example 1 with NamedNativeQuery

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

the class JPAXMLOverriddenAnnotationReader method addNamedNativeQueryIfNeeded.

private void addNamedNativeQueryIfNeeded(NamedNativeQuery annotation, List<NamedNativeQuery> queries) {
    if (annotation != null) {
        String queryName = annotation.name();
        boolean present = false;
        for (NamedNativeQuery current : queries) {
            if (current.name().equals(queryName)) {
                present = true;
                break;
            }
        }
        if (!present) {
            queries.add(annotation);
        }
    }
}
Also used : JaxbNamedNativeQuery(org.hibernate.boot.jaxb.mapping.spi.JaxbNamedNativeQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery)

Example 2 with NamedNativeQuery

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

the class JPAXMLOverriddenAnnotationReader method getNamedNativeQueries.

private NamedNativeQueries getNamedNativeQueries(ManagedType root, XMLContext.Default defaults) {
    List<NamedNativeQuery> queries = root instanceof JaxbEntity ? buildNamedNativeQueries(((JaxbEntity) root).getNamedNativeQuery(), defaults, classLoaderAccess) : new ArrayList<>();
    if (defaults.canUseJavaAnnotations()) {
        NamedNativeQuery annotation = getPhysicalAnnotation(NamedNativeQuery.class);
        addNamedNativeQueryIfNeeded(annotation, queries);
        NamedNativeQueries annotations = getPhysicalAnnotation(NamedNativeQueries.class);
        if (annotations != null) {
            for (NamedNativeQuery current : annotations.value()) {
                addNamedNativeQueryIfNeeded(current, queries);
            }
        }
    }
    if (queries.size() > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(NamedNativeQueries.class);
        ad.setValue("value", queries.toArray(new NamedNativeQuery[queries.size()]));
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbNamedNativeQuery(org.hibernate.boot.jaxb.mapping.spi.JaxbNamedNativeQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) NamedNativeQueries(jakarta.persistence.NamedNativeQueries) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 3 with NamedNativeQuery

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

the class NamedQueryTest method testNativeNamedQueriesOrdinalParametersAreOneBased.

@Test
public void testNativeNamedQueriesOrdinalParametersAreOneBased(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        Query query = session.getNamedNativeQuery("NamedNativeQuery");
        query.setParameter(1, GAME_TITLES[0]);
        List list = query.getResultList();
        assertEquals(1, list.size());
    });
}
Also used : Query(jakarta.persistence.Query) NamedQuery(jakarta.persistence.NamedQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 4 with NamedNativeQuery

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

the class NamedQueryTest method testQueryHintLockMode.

@Test
@TestForIssue(jiraKey = "HHH-14816")
public void testQueryHintLockMode() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedNativeQuery");
        query.setHint(HINT_NATIVE_LOCK_MODE, "none");
        query.setParameter(1, GAME_TITLES[0]);
        assertEquals(LockMode.NONE, query.getHints().get(HINT_NATIVE_LOCK_MODE));
    });
}
Also used : Query(jakarta.persistence.Query) NativeQuery(org.hibernate.query.NativeQuery) NamedQuery(jakarta.persistence.NamedQuery) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with NamedNativeQuery

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

the class NamedQueryTest method testNativeNamedQueriesOrdinalParametersAreOneBased.

@Test
public void testNativeNamedQueriesOrdinalParametersAreOneBased() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedNativeQuery");
        query.setParameter(1, GAME_TITLES[0]);
        List list = query.getResultList();
        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) 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.spi.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.spi.JaxbEntity)1