Search in sources :

Example 1 with NamedQuery

use of javax.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(javax.persistence.Query) NamedNativeQuery(javax.persistence.NamedNativeQuery) NamedQuery(javax.persistence.NamedQuery) List(java.util.List) Test(org.junit.Test)

Example 2 with NamedQuery

use of javax.persistence.NamedQuery in project BroadleafCommerce by BroadleafCommerce.

the class MergePersistenceUnitManager method addNamedQueriesToPersistenceUnits.

protected boolean addNamedQueriesToPersistenceUnits(boolean weaverRegistered) throws Exception {
    // still be transformed by the previous registered transformers
    for (PersistenceUnitInfo pui : mergedPus.values()) {
        // Add annotated named query support from QueryConfiguration beans
        List<NamedQuery> namedQueries = new ArrayList<>();
        List<NamedNativeQuery> nativeQueries = new ArrayList<>();
        for (QueryConfiguration config : queryConfigurations) {
            if (pui.getPersistenceUnitName().equals(config.getPersistenceUnit())) {
                NamedQueries annotation = config.getClass().getAnnotation(NamedQueries.class);
                if (annotation != null) {
                    namedQueries.addAll(Arrays.asList(annotation.value()));
                }
                NamedNativeQueries annotation2 = config.getClass().getAnnotation(NamedNativeQueries.class);
                if (annotation2 != null) {
                    nativeQueries.addAll(Arrays.asList(annotation2.value()));
                }
            }
        }
        if (!namedQueries.isEmpty() || !nativeQueries.isEmpty()) {
            QueryConfigurationClassTransformer transformer = new QueryConfigurationClassTransformer(namedQueries, nativeQueries, pui.getManagedClassNames());
            try {
                pui.addTransformer(transformer);
            } catch (Exception e) {
                weaverRegistered = handleClassTransformerRegistrationProblem(transformer, e);
            }
        }
    }
    return weaverRegistered;
}
Also used : NamedNativeQuery(javax.persistence.NamedNativeQuery) NamedNativeQueries(javax.persistence.NamedNativeQueries) ArrayList(java.util.ArrayList) NamedQueries(javax.persistence.NamedQueries) NamedQuery(javax.persistence.NamedQuery) MutablePersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo)

Example 3 with NamedQuery

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

the class JPAOverriddenAnnotationReader method addNamedQueryIfNeeded.

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

Example 4 with NamedQuery

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

the class NamedQueryTest method testNamedQueriesOrdinalParametersAreZeroBased.

@Test
public void testNamedQueriesOrdinalParametersAreZeroBased() {
    doInHibernate(this::sessionFactory, session -> {
        Query query = session.getNamedQuery("NamedQuery");
        query.setParameter(0, GAME_TITLES[0]);
        List list = query.getResultList();
        assertEquals(1, list.size());
    });
}
Also used : Query(javax.persistence.Query) NamedNativeQuery(javax.persistence.NamedNativeQuery) NamedQuery(javax.persistence.NamedQuery) List(java.util.List) Test(org.junit.Test)

Example 5 with NamedQuery

use of javax.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(javax.persistence.Query) NamedNativeQuery(javax.persistence.NamedNativeQuery) NamedQuery(javax.persistence.NamedQuery) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

NamedQuery (javax.persistence.NamedQuery)10 List (java.util.List)7 NamedNativeQuery (javax.persistence.NamedNativeQuery)7 Query (javax.persistence.Query)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 NamedQueries (javax.persistence.NamedQueries)2 Session (org.hibernate.Session)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Annotation (javassist.bytecode.annotation.Annotation)1 AnnotationMemberValue (javassist.bytecode.annotation.AnnotationMemberValue)1 ArrayMemberValue (javassist.bytecode.annotation.ArrayMemberValue)1 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)1 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1 MemberValue (javassist.bytecode.annotation.MemberValue)1 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)1 LockModeType (javax.persistence.LockModeType)1 NamedNativeQueries (javax.persistence.NamedNativeQueries)1 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)1 QueryHint (javax.persistence.QueryHint)1