Search in sources :

Example 6 with NamedQueryDefinition

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

the class AbstractSharedSessionContract method buildQueryFromName.

protected <T> QueryImplementor<T> buildQueryFromName(String name, Class<T> resultType) {
    checkOpen();
    try {
        checkTransactionSynchStatus();
        delayedAfterCompletion();
        // todo : apply stored setting at the JPA Query level too
        final NamedQueryDefinition namedQueryDefinition = getFactory().getNamedQueryRepository().getNamedQueryDefinition(name);
        if (namedQueryDefinition != null) {
            return createQuery(namedQueryDefinition, resultType);
        }
        final NamedSQLQueryDefinition nativeQueryDefinition = getFactory().getNamedQueryRepository().getNamedSQLQueryDefinition(name);
        if (nativeQueryDefinition != null) {
            return (QueryImplementor<T>) createNativeQuery(nativeQueryDefinition, resultType);
        }
        throw exceptionConverter.convert(new IllegalArgumentException("No query defined for that name [" + name + "]"));
    } catch (RuntimeException e) {
        throw !(e instanceof IllegalArgumentException) ? new IllegalArgumentException(e) : e;
    }
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) NativeQueryImplementor(org.hibernate.query.spi.NativeQueryImplementor) QueryImplementor(org.hibernate.query.spi.QueryImplementor)

Example 7 with NamedQueryDefinition

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

the class NamedQueryRepository method checkNamedQueries.

public Map<String, HibernateException> checkNamedQueries(QueryPlanCache queryPlanCache) {
    Map<String, HibernateException> errors = new HashMap<String, HibernateException>();
    // Check named HQL queries
    log.debugf("Checking %s named HQL queries", namedQueryDefinitionMap.size());
    for (NamedQueryDefinition namedQueryDefinition : namedQueryDefinitionMap.values()) {
        // this will throw an error if there's something wrong.
        try {
            log.debugf("Checking named query: %s", namedQueryDefinition.getName());
            // TODO: BUG! this currently fails for named queries for non-POJO entities
            queryPlanCache.getHQLQueryPlan(namedQueryDefinition.getQueryString(), false, Collections.EMPTY_MAP);
        } catch (HibernateException e) {
            errors.put(namedQueryDefinition.getName(), e);
        }
    }
    // Check native-sql queries
    log.debugf("Checking %s named SQL queries", namedSqlQueryDefinitionMap.size());
    for (NamedSQLQueryDefinition namedSQLQueryDefinition : namedSqlQueryDefinitionMap.values()) {
        // this will throw an error if there's something wrong.
        try {
            log.debugf("Checking named SQL query: %s", namedSQLQueryDefinition.getName());
            // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
            // currently not doable though because of the resultset-ref stuff...
            NativeSQLQuerySpecification spec;
            if (namedSQLQueryDefinition.getResultSetRef() != null) {
                ResultSetMappingDefinition definition = getResultSetMappingDefinition(namedSQLQueryDefinition.getResultSetRef());
                if (definition == null) {
                    throw new MappingException("Unable to find resultset-ref definition: " + namedSQLQueryDefinition.getResultSetRef());
                }
                spec = new NativeSQLQuerySpecification(namedSQLQueryDefinition.getQueryString(), definition.getQueryReturns(), namedSQLQueryDefinition.getQuerySpaces());
            } else {
                spec = new NativeSQLQuerySpecification(namedSQLQueryDefinition.getQueryString(), namedSQLQueryDefinition.getQueryReturns(), namedSQLQueryDefinition.getQuerySpaces());
            }
            queryPlanCache.getNativeSQLQueryPlan(spec);
        } catch (HibernateException e) {
            errors.put(namedSQLQueryDefinition.getName(), e);
        }
    }
    return errors;
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HibernateException(org.hibernate.HibernateException) HashMap(java.util.HashMap) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) NativeSQLQuerySpecification(org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition) MappingException(org.hibernate.MappingException)

Example 8 with NamedQueryDefinition

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

the class NamedQueryRepository method registerNamedQueryDefinition.

public synchronized void registerNamedQueryDefinition(String name, NamedQueryDefinition definition) {
    if (NamedSQLQueryDefinition.class.isInstance(definition)) {
        throw new IllegalArgumentException("NamedSQLQueryDefinition instance incorrectly passed to registerNamedQueryDefinition");
    }
    if (!name.equals(definition.getName())) {
        definition = definition.makeCopy(name);
    }
    final Map<String, NamedQueryDefinition> copy = CollectionHelper.makeCopy(namedQueryDefinitionMap);
    final NamedQueryDefinition previous = copy.put(name, definition);
    if (previous != null) {
        log.debugf("registering named query definition [%s] overriding previously registered definition [%s]", name, previous);
    }
    this.namedQueryDefinitionMap = Collections.unmodifiableMap(copy);
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition)

Example 9 with NamedQueryDefinition

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

the class NamedQueryRepository method registerNamedSQLQueryDefinition.

public synchronized void registerNamedSQLQueryDefinition(String name, NamedSQLQueryDefinition definition) {
    if (!name.equals(definition.getName())) {
        definition = definition.makeCopy(name);
    }
    final Map<String, NamedSQLQueryDefinition> copy = CollectionHelper.makeCopy(namedSqlQueryDefinitionMap);
    final NamedQueryDefinition previous = copy.put(name, definition);
    if (previous != null) {
        log.debugf("registering named SQL query definition [%s] overriding previously registered definition [%s]", name, previous);
    }
    this.namedSqlQueryDefinitionMap = Collections.unmodifiableMap(copy);
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition)

Example 10 with NamedQueryDefinition

use of org.hibernate.engine.spi.NamedQueryDefinition 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

NamedQueryDefinition (org.hibernate.engine.spi.NamedQueryDefinition)10 NamedSQLQueryDefinition (org.hibernate.engine.spi.NamedSQLQueryDefinition)5 EntityManager (javax.persistence.EntityManager)2 Query (javax.persistence.Query)2 AnnotationException (org.hibernate.AnnotationException)2 ResultSetMappingDefinition (org.hibernate.engine.ResultSetMappingDefinition)2 NamedQueryDefinitionBuilder (org.hibernate.engine.spi.NamedQueryDefinitionBuilder)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 FlushMode (org.hibernate.FlushMode)1 HibernateException (org.hibernate.HibernateException)1 MappingException (org.hibernate.MappingException)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 NamedEntityGraphDefinition (org.hibernate.cfg.annotations.NamedEntityGraphDefinition)1 NamedProcedureCallDefinition (org.hibernate.cfg.annotations.NamedProcedureCallDefinition)1 NativeSQLQuerySpecification (org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification)1 NativeQueryImplementor (org.hibernate.query.spi.NativeQueryImplementor)1 QueryImplementor (org.hibernate.query.spi.QueryImplementor)1