Search in sources :

Example 1 with NamedQueryDefinition

use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.

the class QueryBinder method bindQuery.

public static void bindQuery(org.hibernate.annotations.NamedQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    FlushMode flushMode;
    flushMode = getFlushMode(queryAnn.flushMode());
    NamedQueryDefinition query = new NamedQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(flushMode).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).createNamedQueryDefinition();
    context.getMetadataCollector().addNamedQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named query: %s => %s", query.getName(), query.getQueryString());
    }
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) AnnotationException(org.hibernate.AnnotationException) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) FlushMode(org.hibernate.FlushMode)

Example 2 with NamedQueryDefinition

use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.

the class Configuration method reset.

protected void reset() {
    implicitNamingStrategy = ImplicitNamingStrategyJpaCompliantImpl.INSTANCE;
    physicalNamingStrategy = PhysicalNamingStrategyStandardImpl.INSTANCE;
    namedQueries = new HashMap<String, NamedQueryDefinition>();
    namedSqlQueries = new HashMap<String, NamedSQLQueryDefinition>();
    sqlResultSetMappings = new HashMap<String, ResultSetMappingDefinition>();
    namedEntityGraphMap = new HashMap<String, NamedEntityGraphDefinition>();
    namedProcedureCallMap = new HashMap<String, NamedProcedureCallDefinition>();
    standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(bootstrapServiceRegistry);
    entityTuplizerFactory = new EntityTuplizerFactory();
    interceptor = EmptyInterceptor.INSTANCE;
    properties = new Properties();
    properties.putAll(standardServiceRegistryBuilder.getSettings());
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) Properties(java.util.Properties) NamedProcedureCallDefinition(org.hibernate.cfg.annotations.NamedProcedureCallDefinition) NamedEntityGraphDefinition(org.hibernate.cfg.annotations.NamedEntityGraphDefinition) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) EntityTuplizerFactory(org.hibernate.tuple.entity.EntityTuplizerFactory) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition)

Example 3 with NamedQueryDefinition

use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.

the class AddNamedQueryTest method testFlushModeHandling.

@Test
public void testFlushModeHandling() {
    final String name = "flush-mode-handling";
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Query q = em.createQuery("from Item");
    assertEquals(FlushModeType.AUTO, q.getFlushMode());
    q.setFlushMode(FlushModeType.COMMIT);
    assertEquals(FlushModeType.COMMIT, q.getFlushMode());
    em.getEntityManagerFactory().addNamedQuery(name, q);
    // first, lets check the underlying stored query def
    SessionFactoryImplementor sfi = entityManagerFactory().unwrap(SessionFactoryImplementor.class);
    NamedQueryDefinition def = sfi.getNamedQueryRepository().getNamedQueryDefinition(name);
    assertEquals(FlushMode.COMMIT, def.getFlushMode());
    // then lets create a query by name and check its setting
    q = em.createNamedQuery(name);
    assertEquals(FlushMode.COMMIT, q.unwrap(org.hibernate.Query.class).getHibernateFlushMode());
    assertEquals(FlushModeType.COMMIT, q.getFlushMode());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) Test(org.junit.Test)

Example 4 with NamedQueryDefinition

use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.

the class AddNamedQueryTest method testLockModeHandling.

@Test
public void testLockModeHandling() {
    final String name = "lock-mode-handling";
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Query q = em.createQuery("from Item");
    assertEquals(LockModeType.NONE, q.getLockMode());
    q.setLockMode(LockModeType.OPTIMISTIC);
    assertEquals(LockModeType.OPTIMISTIC, q.getLockMode());
    em.getEntityManagerFactory().addNamedQuery(name, q);
    // first, lets check the underlying stored query def
    SessionFactoryImplementor sfi = entityManagerFactory().unwrap(SessionFactoryImplementor.class);
    NamedQueryDefinition def = sfi.getNamedQueryRepository().getNamedQueryDefinition(name);
    assertEquals(LockMode.OPTIMISTIC, def.getLockOptions().getLockMode());
    // then lets create a query by name and check its setting
    q = em.createNamedQuery(name);
    assertEquals(LockMode.OPTIMISTIC, q.unwrap(org.hibernate.Query.class).getLockOptions().getLockMode());
    assertEquals(LockModeType.OPTIMISTIC, q.getLockMode());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) Test(org.junit.Test)

Example 5 with NamedQueryDefinition

use of org.hibernate.engine.spi.NamedQueryDefinition in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method getNamedQuery.

@Override
public QueryImplementor getNamedQuery(String name) {
    checkOpen();
    checkTransactionSynchStatus();
    delayedAfterCompletion();
    // look as HQL/JPQL first
    final NamedQueryDefinition queryDefinition = factory.getNamedQueryRepository().getNamedQueryDefinition(name);
    if (queryDefinition != null) {
        return createQuery(queryDefinition);
    }
    // then as a native query
    final NamedSQLQueryDefinition nativeQueryDefinition = factory.getNamedQueryRepository().getNamedSQLQueryDefinition(name);
    if (nativeQueryDefinition != null) {
        return createNativeQuery(nativeQueryDefinition, true);
    }
    throw exceptionConverter.convert(new IllegalArgumentException("No query defined for that name [" + name + "]"));
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition)

Aggregations

NamedQueryDefinition (org.hibernate.engine.spi.NamedQueryDefinition)10 NamedSQLQueryDefinition (org.hibernate.engine.spi.NamedSQLQueryDefinition)5 EntityManager (javax.persistence.EntityManager)2 Query (javax.persistence.Query)2 AnnotationException (org.hibernate.AnnotationException)2 ResultSetMappingDefinition (org.hibernate.engine.ResultSetMappingDefinition)2 NamedQueryDefinitionBuilder (org.hibernate.engine.spi.NamedQueryDefinitionBuilder)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 FlushMode (org.hibernate.FlushMode)1 HibernateException (org.hibernate.HibernateException)1 MappingException (org.hibernate.MappingException)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 NamedEntityGraphDefinition (org.hibernate.cfg.annotations.NamedEntityGraphDefinition)1 NamedProcedureCallDefinition (org.hibernate.cfg.annotations.NamedProcedureCallDefinition)1 NativeSQLQuerySpecification (org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification)1 NativeQueryImplementor (org.hibernate.query.spi.NativeQueryImplementor)1 QueryImplementor (org.hibernate.query.spi.QueryImplementor)1