Search in sources :

Example 6 with NamedSqmQueryMemento

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;
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedHqlQueryDefinition(org.hibernate.boot.query.NamedHqlQueryDefinition) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento) NamedNativeQueryDefinition(org.hibernate.boot.query.NamedNativeQueryDefinition) NamedQueryMemento(org.hibernate.query.named.NamedQueryMemento) NamedProcedureCallDefinition(org.hibernate.boot.query.NamedProcedureCallDefinition)

Example 7 with NamedSqmQueryMemento

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);
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException)

Example 8 with NamedSqmQueryMemento

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());
    });
}
Also used : Query(jakarta.persistence.Query) NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Test(org.junit.jupiter.api.Test)

Example 9 with NamedSqmQueryMemento

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());
    });
}
Also used : Query(jakarta.persistence.Query) NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Test(org.junit.jupiter.api.Test)

Aggregations

NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)9 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)5 Test (org.junit.jupiter.api.Test)4 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)3 Query (jakarta.persistence.Query)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 NamedCallableQueryMemento (org.hibernate.procedure.spi.NamedCallableQueryMemento)2 UnknownNamedQueryException (org.hibernate.query.UnknownNamedQueryException)2 HashMap (java.util.HashMap)1 HibernateException (org.hibernate.HibernateException)1 NamedHqlQueryDefinition (org.hibernate.boot.query.NamedHqlQueryDefinition)1 NamedNativeQueryDefinition (org.hibernate.boot.query.NamedNativeQueryDefinition)1 NamedProcedureCallDefinition (org.hibernate.boot.query.NamedProcedureCallDefinition)1 HqlTranslator (org.hibernate.query.hql.HqlTranslator)1 NamedQueryMemento (org.hibernate.query.named.NamedQueryMemento)1 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)1 QueryInterpretationCache (org.hibernate.query.spi.QueryInterpretationCache)1