Search in sources :

Example 1 with NamedObjectRepository

use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.

the class Util method resolveResultSetMappingNames.

public static void resolveResultSetMappingNames(String[] resultSetMappingNames, ResultSetMapping resultSetMapping, Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
    final NamedObjectRepository namedObjectRepository = context.getSessionFactory().getQueryEngine().getNamedObjectRepository();
    for (String resultSetMappingName : resultSetMappingNames) {
        final NamedResultSetMappingMemento memento = namedObjectRepository.getResultSetMappingMemento(resultSetMappingName);
        if (memento == null) {
            throw new UnknownSqlResultSetMappingException("Unknown SqlResultSetMapping [" + resultSetMappingName + "]");
        }
        memento.resolve(resultSetMapping, querySpaceConsumer, context);
    }
}
Also used : NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) UnknownSqlResultSetMappingException(org.hibernate.query.UnknownSqlResultSetMappingException) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento)

Example 2 with NamedObjectRepository

use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.

the class NamedRepoTests method testMappingResolution.

@Test
public void testMappingResolution(SessionFactoryScope sessionFactoryScope) {
    final QueryEngine queryEngine = sessionFactoryScope.getSessionFactory().getQueryEngine();
    final NamedObjectRepository namedObjectRepository = queryEngine.getNamedObjectRepository();
    final NamedResultSetMappingMemento mappingMemento = namedObjectRepository.getResultSetMappingMemento("name");
    final ResultSetMapping mapping = new ResultSetMappingImpl("test");
    final ResultSetMappingResolutionContext resolutionContext = new ResultSetMappingResolutionContext() {

        @Override
        public SessionFactoryImplementor getSessionFactory() {
            return sessionFactoryScope.getSessionFactory();
        }
    };
    mappingMemento.resolve(mapping, querySpace -> {
    }, resolutionContext);
    assertThat(mapping.getNumberOfResultBuilders(), is(1));
    mapping.visitResultBuilders((position, builder) -> {
        assertThat(position, is(0));
        assertThat(builder, instanceOf(ResultBuilderBasicValued.class));
    });
}
Also used : ResultBuilderBasicValued(org.hibernate.query.results.ResultBuilderBasicValued) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) ResultSetMapping(org.hibernate.query.results.ResultSetMapping) ResultSetMappingImpl(org.hibernate.query.results.ResultSetMappingImpl) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) ResultSetMappingResolutionContext(org.hibernate.query.internal.ResultSetMappingResolutionContext) QueryEngine(org.hibernate.query.spi.QueryEngine) Test(org.junit.jupiter.api.Test)

Example 3 with NamedObjectRepository

use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.

the class SimpleNamedQueryTests method testBinding.

@Test
public void testBinding(SessionFactoryScope scope) {
    final NamedObjectRepository namedObjectRepository = scope.getSessionFactory().getQueryEngine().getNamedObjectRepository();
    final NamedSqmQueryMemento simpleMemento = namedObjectRepository.getSqmQueryMemento("simple");
    assertThat(simpleMemento, notNullValue());
    final NamedSqmQueryMemento restrictedMemento = namedObjectRepository.getSqmQueryMemento("restricted");
    assertThat(restrictedMemento, notNullValue());
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) Test(org.junit.jupiter.api.Test)

Example 4 with NamedObjectRepository

use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.

the class SimpleNamedQueryTests method testExecution.

@Test
public void testExecution(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        session.createNamedQuery("simple").list();
        session.createNamedQuery("restricted").setParameter("name", "a name").list();
    });
    final NamedObjectRepository namedObjectRepository = scope.getSessionFactory().getQueryEngine().getNamedObjectRepository();
    final NamedSqmQueryMemento simpleMemento = namedObjectRepository.getSqmQueryMemento("simple");
    assertThat(simpleMemento, notNullValue());
    final NamedSqmQueryMemento restrictedMemento = namedObjectRepository.getSqmQueryMemento("restricted");
    assertThat(restrictedMemento, notNullValue());
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) Test(org.junit.jupiter.api.Test)

Example 5 with NamedObjectRepository

use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.

the class SessionFactoryImpl method addNamedQuery.

@Override
public void addNamedQuery(String name, Query query) {
    validateNotClosed();
    // NOTE : we use Query#unwrap here (rather than direct type checking) to account for possibly wrapped
    // query implementations
    // first, handle StoredProcedureQuery
    final NamedObjectRepository namedObjectRepository = getQueryEngine().getNamedObjectRepository();
    try {
        final ProcedureCallImplementor<?> unwrapped = query.unwrap(ProcedureCallImplementor.class);
        if (unwrapped != null) {
            namedObjectRepository.registerCallableQueryMemento(name, unwrapped.toMemento(name));
            return;
        }
    } catch (PersistenceException ignore) {
    // this means 'query' is not a ProcedureCallImplementor
    }
    // then try as a native-SQL or JPQL query
    try {
        QueryImplementor<?> hibernateQuery = query.unwrap(QueryImplementor.class);
        if (hibernateQuery != null) {
            // create and register the proper NamedQueryDefinition...
            if (hibernateQuery instanceof NativeQueryImplementor) {
                namedObjectRepository.registerNativeQueryMemento(name, ((NativeQueryImplementor<?>) hibernateQuery).toMemento(name));
            } else {
                final NamedQueryMemento namedQueryMemento = ((SqmQueryImplementor<?>) hibernateQuery).toMemento(name);
                namedObjectRepository.registerSqmQueryMemento(name, (NamedSqmQueryMemento) namedQueryMemento);
            }
            return;
        }
    } catch (PersistenceException ignore) {
    // this means 'query' is not a native-SQL or JPQL query
    }
    // if we get here, we are unsure how to properly unwrap the incoming query to extract the needed information
    throw new PersistenceException(String.format("Unsure how to properly unwrap given Query [%s] as basis for named query", query));
}
Also used : NativeQueryImplementor(org.hibernate.query.sql.spi.NativeQueryImplementor) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) PersistenceException(jakarta.persistence.PersistenceException) NamedQueryMemento(org.hibernate.query.named.NamedQueryMemento)

Aggregations

NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)6 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)3 Test (org.junit.jupiter.api.Test)3 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)2 PersistenceException (jakarta.persistence.PersistenceException)1 UnknownNamedQueryException (org.hibernate.query.UnknownNamedQueryException)1 UnknownSqlResultSetMappingException (org.hibernate.query.UnknownSqlResultSetMappingException)1 SqmQueryImplementor (org.hibernate.query.hql.spi.SqmQueryImplementor)1 ResultSetMappingResolutionContext (org.hibernate.query.internal.ResultSetMappingResolutionContext)1 NamedQueryMemento (org.hibernate.query.named.NamedQueryMemento)1 ResultBuilderBasicValued (org.hibernate.query.results.ResultBuilderBasicValued)1 ResultSetMapping (org.hibernate.query.results.ResultSetMapping)1 ResultSetMappingImpl (org.hibernate.query.results.ResultSetMappingImpl)1 QueryEngine (org.hibernate.query.spi.QueryEngine)1 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)1 NativeQueryImplementor (org.hibernate.query.sql.spi.NativeQueryImplementor)1