Search in sources :

Example 1 with NamedResultSetMappingMemento

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

the class NamedObjectRepositoryImpl method prepare.

@Override
public void prepare(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, BootstrapContext bootstrapContext) {
    bootMetamodel.visitNamedHqlQueryDefinitions(namedHqlQueryDefinition -> {
        final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
        sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
    });
    bootMetamodel.visitNamedNativeQueryDefinitions(namedNativeQueryDefinition -> {
        final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
        sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
    });
    bootMetamodel.visitNamedResultSetMappingDefinition(namedResultSetMappingDefinition -> {
        final NamedResultSetMappingMemento resolved = namedResultSetMappingDefinition.resolve(() -> sessionFactory);
        resultSetMappingMementoMap.put(namedResultSetMappingDefinition.getRegistrationName(), resolved);
    });
    bootMetamodel.visitNamedProcedureCallDefinition(namedProcedureCallDefinition -> {
        final NamedCallableQueryMemento resolved = namedProcedureCallDefinition.resolve(sessionFactory);
        callableMementoMap.put(namedProcedureCallDefinition.getRegistrationName(), resolved);
    });
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento)

Example 2 with NamedResultSetMappingMemento

use of org.hibernate.query.named.NamedResultSetMappingMemento 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 3 with NamedResultSetMappingMemento

use of org.hibernate.query.named.NamedResultSetMappingMemento 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 4 with NamedResultSetMappingMemento

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

the class BasicResultTests method testSimpleScalarMapping.

@Test
public void testSimpleScalarMapping(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        // make sure it is in the repository
        final NamedResultSetMappingMemento mappingMemento = session.getSessionFactory().getQueryEngine().getNamedObjectRepository().getResultSetMappingMemento("name");
        assertThat(mappingMemento, notNullValue());
        // apply it to a native-query
        final String qryString = "select name from SimpleEntityWithNamedMappings";
        final List<String> names = session.createNativeQuery(qryString, "name").list();
        assertThat(names.size(), is(1));
        assertThat(names.get(0), is("test"));
        // todo (6.0) : should also try executing the ProcedureCall once that functionality is implemented
        session.createStoredProcedureCall("abc", "name");
    });
}
Also used : NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) Test(org.junit.jupiter.api.Test)

Example 5 with NamedResultSetMappingMemento

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

the class EntityResultTests method testSimpleEntityResultMapping.

@Test
public void testSimpleEntityResultMapping(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        // make sure it is in the repository
        final NamedResultSetMappingMemento mappingMemento = session.getSessionFactory().getQueryEngine().getNamedObjectRepository().getResultSetMappingMemento("entity");
        assertThat(mappingMemento, notNullValue());
        // apply it to a native-query
        final String qryString = "select id, name, notes from SimpleEntityWithNamedMappings";
        final List<SimpleEntityWithNamedMappings> results = session.createNativeQuery(qryString, "entity").list();
        assertThat(results.size(), is(1));
        final SimpleEntityWithNamedMappings entity = results.get(0);
        assertThat(entity.getId(), is(1));
        assertThat(entity.getName(), is("test"));
        assertThat(entity.getNotes(), is("notes"));
        // todo (6.0) : should also try executing the ProcedureCall once that functionality is implemented
        session.createStoredProcedureCall("abc", "entity");
    });
}
Also used : NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) Test(org.junit.jupiter.api.Test)

Aggregations

NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)10 Test (org.junit.jupiter.api.Test)7 ResultSetMappingResolutionContext (org.hibernate.query.internal.ResultSetMappingResolutionContext)2 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)2 ResultSetMapping (org.hibernate.query.results.ResultSetMapping)2 ResultSetMappingImpl (org.hibernate.query.results.ResultSetMappingImpl)2 StringReader (java.io.StringReader)1 Configuration (org.hibernate.cfg.Configuration)1 ReaderInputStream (org.hibernate.engine.jdbc.ReaderInputStream)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 NamedCallableQueryMemento (org.hibernate.procedure.spi.NamedCallableQueryMemento)1 UnknownSqlResultSetMappingException (org.hibernate.query.UnknownSqlResultSetMappingException)1 ResultBuilderBasicValued (org.hibernate.query.results.ResultBuilderBasicValued)1 CompleteResultBuilderEntityValued (org.hibernate.query.results.complete.CompleteResultBuilderEntityValued)1 QueryEngine (org.hibernate.query.spi.QueryEngine)1 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)1 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)1 Test (org.junit.Test)1