Search in sources :

Example 1 with NamedNativeQuery

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

the class NamedQueryTest method testNativeNamedQueriesOrdinalParametersAreZeroBased.

@Test
public void testNativeNamedQueriesOrdinalParametersAreZeroBased() {
    doInHibernate(this::sessionFactory, session -> {
        Query query = session.getNamedNativeQuery("NamedNativeQuery");
        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 2 with NamedNativeQuery

use of javax.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(0, 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)

Example 3 with NamedNativeQuery

use of javax.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.createSQLQuery("select * from Game g where title = ?");
        sessionQuery.setParameter(0, 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)

Example 4 with NamedNativeQuery

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

Example 5 with NamedNativeQuery

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

the class JPAMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (defaults == null) {
        defaults = new HashMap<Object, Object>();
        XMLContext.Default xmlDefaults = xmlContext.getDefault(null);
        defaults.put("schema", xmlDefaults.getSchema());
        defaults.put("catalog", xmlDefaults.getCatalog());
        defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
        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 (Element element : xmlContext.getAllDocuments()) {
            @SuppressWarnings("unchecked") List<Element> elements = element.elements("sequence-generator");
            List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
            if (sequenceGenerators == null) {
                sequenceGenerators = new ArrayList<SequenceGenerator>();
                defaults.put(SequenceGenerator.class, sequenceGenerators);
            }
            for (Element subelement : elements) {
                sequenceGenerators.add(JPAOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(subelement));
            }
            elements = element.elements("table-generator");
            List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
            if (tableGenerators == null) {
                tableGenerators = new ArrayList<TableGenerator>();
                defaults.put(TableGenerator.class, tableGenerators);
            }
            for (Element subelement : elements) {
                tableGenerators.add(JPAOverriddenAnnotationReader.buildTableGeneratorAnnotation(subelement, xmlDefaults));
            }
            List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
            if (namedQueries == null) {
                namedQueries = new ArrayList<NamedQuery>();
                defaults.put(NamedQuery.class, namedQueries);
            }
            List<NamedQuery> currentNamedQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, false, xmlDefaults, classLoaderAccess);
            namedQueries.addAll(currentNamedQueries);
            List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
            if (namedNativeQueries == null) {
                namedNativeQueries = new ArrayList<NamedNativeQuery>();
                defaults.put(NamedNativeQuery.class, namedNativeQueries);
            }
            List<NamedNativeQuery> currentNamedNativeQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, true, xmlDefaults, classLoaderAccess);
            namedNativeQueries.addAll(currentNamedNativeQueries);
            List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
            if (sqlResultSetMappings == null) {
                sqlResultSetMappings = new ArrayList<SqlResultSetMapping>();
                defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
            }
            List<SqlResultSetMapping> currentSqlResultSetMappings = JPAOverriddenAnnotationReader.buildSqlResultsetMappings(element, xmlDefaults, classLoaderAccess);
            sqlResultSetMappings.addAll(currentSqlResultSetMappings);
            List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
            if (namedStoredProcedureQueries == null) {
                namedStoredProcedureQueries = new ArrayList<NamedStoredProcedureQuery>();
                defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
            }
            List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAOverriddenAnnotationReader.buildNamedStoreProcedureQueries(element, xmlDefaults, classLoaderAccess);
            namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
        }
    }
    return defaults;
}
Also used : NamedNativeQuery(javax.persistence.NamedNativeQuery) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) SequenceGenerator(javax.persistence.SequenceGenerator) TableGenerator(javax.persistence.TableGenerator) NamedQuery(javax.persistence.NamedQuery) SqlResultSetMapping(javax.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Aggregations

NamedNativeQuery (javax.persistence.NamedNativeQuery)7 List (java.util.List)6 NamedQuery (javax.persistence.NamedQuery)5 Query (javax.persistence.Query)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 Session (org.hibernate.Session)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 NamedNativeQueries (javax.persistence.NamedNativeQueries)1 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)1 SequenceGenerator (javax.persistence.SequenceGenerator)1 SqlResultSetMapping (javax.persistence.SqlResultSetMapping)1 TableGenerator (javax.persistence.TableGenerator)1 Element (org.dom4j.Element)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1