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());
}
}
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());
}
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();
}
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();
}
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 + "]"));
}
Aggregations