Search in sources :

Example 1 with NamedQueryDefinitionBuilder

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

the class QueryBinder method bindQuery.

public static void bindQuery(org.hibernate.annotations.NamedQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    FlushMode flushMode;
    flushMode = getFlushMode(queryAnn.flushMode());
    NamedQueryDefinition query = new NamedQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).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(flushMode).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).createNamedQueryDefinition();
    context.getMetadataCollector().addNamedQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named query: %s => %s", query.getName(), query.getQueryString());
    }
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) AnnotationException(org.hibernate.AnnotationException) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) FlushMode(org.hibernate.FlushMode)

Example 2 with NamedQueryDefinitionBuilder

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

the class SessionFactoryImpl method extractHqlQueryDefinition.

private NamedQueryDefinition extractHqlQueryDefinition(org.hibernate.query.Query hqlQuery, String name) {
    final NamedQueryDefinitionBuilder builder = new NamedQueryDefinitionBuilder(name);
    fillInNamedQueryBuilder(builder, hqlQuery);
    // LockOptions only valid for HQL/JPQL queries...
    builder.setLockOptions(hqlQuery.getLockOptions().makeCopy());
    return builder.createNamedQueryDefinition();
}
Also used : NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder)

Example 3 with NamedQueryDefinitionBuilder

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

the class NamedQueryBinder method processNamedQuery.

public static void processNamedQuery(HbmLocalMetadataBuildingContext context, JaxbHbmNamedQueryType namedQueryBinding, String prefix) {
    String query = null;
    java.util.Map<String, String> parameterTypeMap = null;
    for (Object content : namedQueryBinding.getContent()) {
        if (String.class.isInstance(content)) {
            String trimmed = ((String) content).trim();
            if (!"".equals(trimmed)) {
                query = trimmed;
            }
        } else {
            final JaxbHbmQueryParamType paramTypeBinding = (JaxbHbmQueryParamType) ((JAXBElement) content).getValue();
            if (parameterTypeMap == null) {
                parameterTypeMap = new HashMap<String, String>();
            }
            parameterTypeMap.put(paramTypeBinding.getName(), paramTypeBinding.getType());
        }
    }
    if (query == null) {
        throw new org.hibernate.boot.MappingException(String.format("Named query [%s] did not specify query string", namedQueryBinding.getName()), context.getOrigin());
    }
    context.getMetadataCollector().addNamedQuery(new NamedQueryDefinitionBuilder().setName(prefix + namedQueryBinding.getName()).setQuery(query).setComment(namedQueryBinding.getComment()).setCacheable(namedQueryBinding.isCacheable()).setCacheMode(namedQueryBinding.getCacheMode()).setCacheRegion(namedQueryBinding.getCacheRegion()).setTimeout(namedQueryBinding.getTimeout()).setReadOnly(namedQueryBinding.isReadOnly()).setFlushMode(namedQueryBinding.getFlushMode()).setFetchSize(namedQueryBinding.getFetchSize()).setParameterTypes(parameterTypeMap).createNamedQueryDefinition());
}
Also used : JaxbHbmQueryParamType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmQueryParamType) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) MappingException(org.hibernate.MappingException)

Example 4 with NamedQueryDefinitionBuilder

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

the class QueryBinder method bindQuery.

public static void bindQuery(NamedQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
    if (queryAnn == null)
        return;
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    // EJBQL Query
    QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
    String queryName = queryAnn.query();
    NamedQueryDefinition queryDefinition = new NamedQueryDefinitionBuilder(queryAnn.name()).setLockOptions(hints.determineLockOptions(queryAnn)).setQuery(queryName).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).createNamedQueryDefinition();
    if (isDefault) {
        context.getMetadataCollector().addDefaultQuery(queryDefinition);
    } else {
        context.getMetadataCollector().addNamedQuery(queryDefinition);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named query: %s => %s", queryDefinition.getName(), queryDefinition.getQueryString());
    }
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) AnnotationException(org.hibernate.AnnotationException) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder)

Aggregations

NamedQueryDefinitionBuilder (org.hibernate.engine.spi.NamedQueryDefinitionBuilder)4 AnnotationException (org.hibernate.AnnotationException)2 NamedQueryDefinition (org.hibernate.engine.spi.NamedQueryDefinition)2 FlushMode (org.hibernate.FlushMode)1 MappingException (org.hibernate.MappingException)1 JaxbHbmQueryParamType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmQueryParamType)1