Search in sources :

Example 1 with NamedSQLQueryDefinitionBuilder

use of org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder in project hibernate-orm by hibernate.

the class NamedQueryBinder method processNamedNativeQuery.

public static void processNamedNativeQuery(final HbmLocalMetadataBuildingContext context, JaxbHbmNamedNativeQueryType namedQueryBinding, String prefix) {
    final String queryName = prefix + namedQueryBinding.getName();
    final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder().setName(queryName).setComment(namedQueryBinding.getComment()).setCacheable(namedQueryBinding.isCacheable()).setCacheMode(namedQueryBinding.getCacheMode()).setCacheRegion(namedQueryBinding.getCacheRegion()).setTimeout(namedQueryBinding.getTimeout()).setReadOnly(namedQueryBinding.isReadOnly()).setFlushMode(namedQueryBinding.getFlushMode()).setFetchSize(namedQueryBinding.getFetchSize()).setCallable(namedQueryBinding.isCallable()).setResultSetRef(namedQueryBinding.getResultsetRef());
    final ImplicitResultSetMappingDefinition.Builder implicitResultSetMappingBuilder = new ImplicitResultSetMappingDefinition.Builder(queryName);
    boolean foundQuery = false;
    for (Object content : namedQueryBinding.getContent()) {
        final boolean wasQuery = processNamedQueryContentItem(content, builder, implicitResultSetMappingBuilder, namedQueryBinding, context);
        if (wasQuery) {
            foundQuery = true;
        }
    }
    if (!foundQuery) {
        throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] did not specify query string", namedQueryBinding.getName()), context.getOrigin());
    }
    if (implicitResultSetMappingBuilder.hasAnyReturns()) {
        if (StringHelper.isNotEmpty(namedQueryBinding.getResultsetRef())) {
            throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] specified both a resultset-ref and an inline mapping of results", namedQueryBinding.getName()), context.getOrigin());
        }
        // Building a ResultSet mapping needs access to entity bindings for any entity
        // returns it defines.  But binding for those entities may have not been
        // completed yet.  For "normal" ResultSet mappings, this is already handled by
        // the fact that MetadataSourceProcessor#processResultSetMappings() is called
        // afterQuery all entity hierarchies have been processed.  However, here we are in
        // the middle of processing named-queries (either top-level or entity-level)
        // and have no guarantee that any entity bindings we may need here are bound.
        // So we add the second-pass to bind the implicit resultSet mapping.
        //
        // It is possible to know here whether the second-pass is needed or whether we
        // can immediately bind the ResultSet mapping.
        // todo : consider implementing this (^^) checking
        final ImplicitResultSetMappingDefinition implicitResultSetMappingDefinition = implicitResultSetMappingBuilder.build();
        builder.setResultSetRef(implicitResultSetMappingDefinition.getName());
        context.getMetadataCollector().addSecondPass(new SecondPass() {

            @Override
            public void doSecondPass(Map persistentClasses) throws MappingException {
                final ResultSetMappingDefinition resultSetMappingDefinition = ResultSetMappingBinder.bind(implicitResultSetMappingDefinition, context);
                context.getMetadataCollector().addResultSetMapping(resultSetMappingDefinition);
                NativeSQLQueryReturn[] newQueryReturns = resultSetMappingDefinition.getQueryReturns();
                final NamedSQLQueryDefinition queryDefinition = context.getMetadataCollector().getNamedNativeQueryDefinition(queryName);
                if (queryDefinition != null) {
                    queryDefinition.addQueryReturns(newQueryReturns);
                }
            }
        });
    }
    context.getMetadataCollector().addNamedNativeQuery(builder.createNamedQueryDefinition());
}
Also used : NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) ImplicitResultSetMappingDefinition(org.hibernate.boot.jaxb.hbm.internal.ImplicitResultSetMappingDefinition) MappingException(org.hibernate.MappingException) NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) SecondPass(org.hibernate.cfg.SecondPass) HashMap(java.util.HashMap) Map(java.util.Map) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition) ImplicitResultSetMappingDefinition(org.hibernate.boot.jaxb.hbm.internal.ImplicitResultSetMappingDefinition)

Example 2 with NamedSQLQueryDefinitionBuilder

use of org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    //ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    NamedSQLQueryDefinition query;
    String resultSetMapping = queryAnn.resultSetMapping();
    if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
        //sql result set usage
        query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setResultSetRef(resultSetMapping).setQuerySpaces(null).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(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
    } else if (!void.class.equals(queryAnn.resultClass())) {
        //class mapping usage
        //FIXME should be done in a second pass due to entity name?
        final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
        query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn }).setQuerySpaces(null).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(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
    } else {
        throw new NotYetImplementedException("Pure native scalar queries are not yet supported");
    }
    context.getMetadataCollector().addNamedNativeQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named native query: %s => %s", query.getName(), queryAnn.query());
    }
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HashMap(java.util.HashMap) AnnotationException(org.hibernate.AnnotationException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) NotYetImplementedException(org.hibernate.cfg.NotYetImplementedException)

Example 3 with NamedSQLQueryDefinitionBuilder

use of org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(NamedNativeQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
    if (queryAnn == null)
        return;
    //ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    String resultSetMapping = queryAnn.resultSetMapping();
    QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
    String queryName = queryAnn.query();
    NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(queryAnn.name()).setQuery(queryName).setQuerySpaces(null).setCacheable(hints.getBoolean(queryName, QueryHints.CACHEABLE)).setCacheRegion(hints.getString(queryName, QueryHints.CACHE_REGION)).setTimeout(hints.getTimeout(queryName)).setFetchSize(hints.getInteger(queryName, QueryHints.FETCH_SIZE)).setFlushMode(hints.getFlushMode(queryName)).setCacheMode(hints.getCacheMode(queryName)).setReadOnly(hints.getBoolean(queryName, QueryHints.READ_ONLY)).setComment(hints.getString(queryName, QueryHints.COMMENT)).setParameterTypes(null).setCallable(hints.getBoolean(queryName, QueryHints.CALLABLE));
    if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
        //sql result set usage
        builder.setResultSetRef(resultSetMapping).createNamedQueryDefinition();
    } else if (!void.class.equals(queryAnn.resultClass())) {
        //class mapping usage
        //FIXME should be done in a second pass due to entity name?
        final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
        builder.setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn });
    } else {
        builder.setQueryReturns(new NativeSQLQueryReturn[0]);
    }
    NamedSQLQueryDefinition query = builder.createNamedQueryDefinition();
    if (isDefault) {
        context.getMetadataCollector().addDefaultNamedNativeQuery(query);
    } else {
        context.getMetadataCollector().addNamedNativeQuery(query);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named native query: %s => %s", queryAnn.name(), queryAnn.query());
    }
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HashMap(java.util.HashMap) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) AnnotationException(org.hibernate.AnnotationException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder)

Example 4 with NamedSQLQueryDefinitionBuilder

use of org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder in project hibernate-orm by hibernate.

the class SessionFactoryImpl method extractSqlQueryDefinition.

private NamedSQLQueryDefinition extractSqlQueryDefinition(org.hibernate.query.NativeQuery nativeSqlQuery, String name) {
    final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(name);
    fillInNamedQueryBuilder(builder, nativeSqlQuery);
    builder.setCallable(nativeSqlQuery.isCallable()).setQuerySpaces(nativeSqlQuery.getSynchronizedQuerySpaces()).setQueryReturns(nativeSqlQuery.getQueryReturns());
    return builder.createNamedQueryDefinition();
}
Also used : NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder)

Example 5 with NamedSQLQueryDefinitionBuilder

use of org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder in project hibernate-orm by hibernate.

the class NativeSQLQueriesTest method testRegisteredNamedSQLQueryWithScalar.

@Test
public void testRegisteredNamedSQLQueryWithScalar() {
    final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder();
    builder.setName("namedQuery");
    builder.setQuery("select count(*) AS c from ORGANIZATION");
    builder.setQueryReturns(new NativeSQLQueryReturn[1]);
    sessionFactory().registerNamedSQLQueryDefinition("namedQuery", builder.createNamedQueryDefinition());
    final Session s = openSession();
    s.beginTransaction();
    final SQLQuery query = (SQLQuery) s.getNamedQuery("namedQuery");
    query.addScalar("c");
    final Number result = (Number) query.uniqueResult();
    s.getTransaction().commit();
    s.close();
    assertNotNull(result);
    assertTrue(0 == result.intValue());
}
Also used : SQLQuery(org.hibernate.SQLQuery) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

NamedSQLQueryDefinitionBuilder (org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder)5 HashMap (java.util.HashMap)3 NamedSQLQueryDefinition (org.hibernate.engine.spi.NamedSQLQueryDefinition)3 AnnotationException (org.hibernate.AnnotationException)2 NativeSQLQueryRootReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn)2 Map (java.util.Map)1 MappingException (org.hibernate.MappingException)1 SQLQuery (org.hibernate.SQLQuery)1 Session (org.hibernate.Session)1 ImplicitResultSetMappingDefinition (org.hibernate.boot.jaxb.hbm.internal.ImplicitResultSetMappingDefinition)1 NotYetImplementedException (org.hibernate.cfg.NotYetImplementedException)1 SecondPass (org.hibernate.cfg.SecondPass)1 ResultSetMappingDefinition (org.hibernate.engine.ResultSetMappingDefinition)1 NativeSQLQueryReturn (org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn)1 NamedQueryDefinitionBuilder (org.hibernate.engine.spi.NamedQueryDefinitionBuilder)1 Test (org.junit.Test)1