use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class NamedObjectRepositoryImpl method resolve.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Prepare repository for use
@Override
public NamedQueryMemento resolve(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, String registrationName) {
NamedQueryMemento namedQuery = sqlMementoMap.get(registrationName);
if (namedQuery != null) {
return namedQuery;
}
namedQuery = sqmMementoMap.get(registrationName);
if (namedQuery != null) {
return namedQuery;
}
namedQuery = callableMementoMap.get(registrationName);
if (namedQuery != null) {
return namedQuery;
}
final NamedHqlQueryDefinition namedHqlQueryDefinition = bootMetamodel.getNamedHqlQueryMapping(registrationName);
if (namedHqlQueryDefinition != null) {
final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
return resolved;
}
final NamedNativeQueryDefinition namedNativeQueryDefinition = bootMetamodel.getNamedNativeQueryMapping(registrationName);
if (namedNativeQueryDefinition != null) {
final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
return resolved;
}
final NamedProcedureCallDefinition namedCallableQueryDefinition = bootMetamodel.getNamedProcedureCallMapping(registrationName);
if (namedCallableQueryDefinition != null) {
final NamedCallableQueryMemento resolved = namedCallableQueryDefinition.resolve(sessionFactory);
callableMementoMap.put(namedCallableQueryDefinition.getRegistrationName(), resolved);
return resolved;
}
return null;
}
use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method buildNamedQuery.
@SuppressWarnings("rawtypes")
protected QueryImplementor buildNamedQuery(String queryName, Function<NamedSqmQueryMemento, SqmQueryImplementor> namedSqmHandler, Function<NamedNativeQueryMemento, NativeQueryImplementor> namedNativeHandler) {
checkOpen();
pulseTransactionCoordinator();
delayedAfterCompletion();
// this method can be called for either a named HQL query or a named native query
// first see if it is a named HQL query
final NamedObjectRepository namedObjectRepository = getFactory().getQueryEngine().getNamedObjectRepository();
final NamedSqmQueryMemento namedSqmQueryMemento = namedObjectRepository.getSqmQueryMemento(queryName);
if (namedSqmQueryMemento != null) {
return namedSqmHandler.apply(namedSqmQueryMemento);
}
// otherwise, see if it is a named native query
final NamedNativeQueryMemento namedNativeDescriptor = namedObjectRepository.getNativeQueryMemento(queryName);
if (namedNativeDescriptor != null) {
return namedNativeHandler.apply(namedNativeDescriptor);
}
throw new UnknownNamedQueryException(queryName);
}
use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class AddNamedQueryTest method testFlushModeHandling.
@Test
public void testFlushModeHandling(EntityManagerFactoryScope scope) {
final String name = "flush-mode-handling";
scope.inTransaction(em -> {
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 = scope.getEntityManagerFactory().unwrap(SessionFactoryImplementor.class);
NamedSqmQueryMemento def = sfi.getQueryEngine().getNamedObjectRepository().getSqmQueryMemento(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.Query.class).getHibernateFlushMode());
assertEquals(FlushModeType.COMMIT, q.getFlushMode());
});
}
use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class AddNamedQueryTest method testLockModeHandling.
@Test
public void testLockModeHandling(EntityManagerFactoryScope scope) {
final String name = "lock-mode-handling";
scope.inTransaction(em -> {
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 = scope.getEntityManagerFactory().unwrap(SessionFactoryImplementor.class);
NamedSqmQueryMemento def = sfi.getQueryEngine().getNamedObjectRepository().getSqmQueryMemento(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.Query.class).getLockOptions().getLockMode());
assertEquals(LockModeType.OPTIMISTIC, q.getLockMode());
});
}
Aggregations