Search in sources :

Example 6 with NamedQuery

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

the class NamedQueryTest method testNamedQueryOrdinalParametersConflict.

@Test
public void testNamedQueryOrdinalParametersConflict() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedQuery");
        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.createQuery("select g from Game g where title = ?1");
        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 7 with NamedQuery

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

the class NamedQueryTest method testNamedQueriesOrdinalParametersAreOneBased.

@Test
public void testNamedQueriesOrdinalParametersAreOneBased() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedQuery");
        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)

Example 8 with NamedQuery

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

the class NamedQueryTest method testNamedQueryOrdinalParametersConflict2.

@Test
public void testNamedQueryOrdinalParametersConflict2() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("NamedQuery");
        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.getNamedQuery("NamedQuery");
        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 NamedQuery

use of jakarta.persistence.NamedQuery in project eclipselink by eclipse-ee4j.

the class JavaEntity method buildQueries.

protected Map<String, IQuery> buildQueries() {
    Map<String, IQuery> queries = new HashMap<>();
    try {
        Class<?> type = getType().getType();
        Annotation[] annotations = type.getAnnotations();
        NamedQueries namedQueries = getAnnotation(annotations, NamedQueries.class);
        if (namedQueries != null) {
            for (NamedQuery namedQuery : namedQueries.value()) {
                IQuery query = buildQuery(namedQuery);
                queries.put(namedQuery.name(), query);
            }
        } else {
            NamedQuery namedQuery = getAnnotation(annotations, NamedQuery.class);
            if (namedQuery != null) {
                IQuery query = buildQuery(namedQuery);
                queries.put(namedQuery.name(), query);
            }
        }
    } catch (Exception e) {
    // Ignore
    }
    return queries;
}
Also used : IQuery(org.eclipse.persistence.jpa.jpql.tools.spi.IQuery) HashMap(java.util.HashMap) NamedQueries(jakarta.persistence.NamedQueries) NamedQuery(jakarta.persistence.NamedQuery) Annotation(java.lang.annotation.Annotation)

Aggregations

NamedQuery (jakarta.persistence.NamedQuery)9 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)5 List (java.util.List)5 Query (jakarta.persistence.Query)4 JaxbNamedQuery (org.hibernate.boot.jaxb.mapping.spi.JaxbNamedQuery)3 NativeQuery (org.hibernate.query.NativeQuery)3 Test (org.junit.Test)3 NamedQueries (jakarta.persistence.NamedQueries)2 ArrayList (java.util.ArrayList)2 Session (org.hibernate.Session)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)1 SequenceGenerator (jakarta.persistence.SequenceGenerator)1 SqlResultSetMapping (jakarta.persistence.SqlResultSetMapping)1 TableGenerator (jakarta.persistence.TableGenerator)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 IQuery (org.eclipse.persistence.jpa.jpql.tools.spi.IQuery)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 JaxbEntityMappings (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings)1