Search in sources :

Example 1 with UnknownNamedQueryException

use of org.hibernate.query.UnknownNamedQueryException in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method createNamedSelectionQuery.

@Override
public <R> SelectionQuery<R> createNamedSelectionQuery(String queryName, Class<R> expectedResultType) {
    checkOpen();
    pulseTransactionCoordinator();
    delayedAfterCompletion();
    // this method can be called for either a named HQL query or a named native query
    // first see if it is a named HQL query
    final NamedSqmQueryMemento namedHqlDescriptor = getFactory().getQueryEngine().getNamedObjectRepository().getSqmQueryMemento(queryName);
    if (namedHqlDescriptor != null) {
        return createNamedSqmSelectionQuery(namedHqlDescriptor, expectedResultType);
    }
    // otherwise, see if it is a named native query
    final NamedNativeQueryMemento namedNativeDescriptor = getFactory().getQueryEngine().getNamedObjectRepository().getNativeQueryMemento(queryName);
    if (namedNativeDescriptor != null) {
        return createNamedNativeSelectionQuery(namedNativeDescriptor, expectedResultType);
    }
    throw new UnknownNamedQueryException(queryName);
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException)

Example 2 with UnknownNamedQueryException

use of org.hibernate.query.UnknownNamedQueryException in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method buildNamedQuery.

@SuppressWarnings("rawtypes")
protected QueryImplementor buildNamedQuery(String queryName, Function<NamedSqmQueryMemento, SqmQueryImplementor> namedSqmHandler, Function<NamedNativeQueryMemento, NativeQueryImplementor> namedNativeHandler) {
    checkOpen();
    pulseTransactionCoordinator();
    delayedAfterCompletion();
    // this method can be called for either a named HQL query or a named native query
    // first see if it is a named HQL query
    final NamedObjectRepository namedObjectRepository = getFactory().getQueryEngine().getNamedObjectRepository();
    final NamedSqmQueryMemento namedSqmQueryMemento = namedObjectRepository.getSqmQueryMemento(queryName);
    if (namedSqmQueryMemento != null) {
        return namedSqmHandler.apply(namedSqmQueryMemento);
    }
    // otherwise, see if it is a named native query
    final NamedNativeQueryMemento namedNativeDescriptor = namedObjectRepository.getNativeQueryMemento(queryName);
    if (namedNativeDescriptor != null) {
        return namedNativeHandler.apply(namedNativeDescriptor);
    }
    throw new UnknownNamedQueryException(queryName);
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException)

Example 3 with UnknownNamedQueryException

use of org.hibernate.query.UnknownNamedQueryException in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method buildNamedQuery.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected <T> QueryImplementor<T> buildNamedQuery(String queryName, Class<T> resultType) {
    try {
        return buildNamedQuery(queryName, (memento) -> {
            final SqmQueryImplementor query = memento.toQuery(this, resultType);
            if (StringHelper.isEmpty(query.getComment())) {
                query.setComment("dynamic query");
            }
            applyQuerySettingsAndHints(query);
            if (memento.getLockOptions() != null) {
                query.setLockOptions(memento.getLockOptions());
            }
            return query;
        }, (memento) -> {
            final NativeQueryImplementor query;
            if (resultType == null) {
                query = memento.toQuery(this);
            } else {
                query = memento.toQuery(this, resultType);
            }
            if (StringHelper.isEmpty(query.getComment())) {
                query.setComment("dynamic native-SQL query");
            }
            applyQuerySettingsAndHints(query);
            return query;
        });
    } catch (UnknownNamedQueryException e) {
        // JPA expects this to mark the transaction for rollback only
        transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
        // it also expects an IllegalArgumentException, so wrap UnknownNamedQueryException
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (RuntimeException e) {
        throw getExceptionConverter().convert(e);
    }
}
Also used : SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) NativeQueryImplementor(org.hibernate.query.sql.spi.NativeQueryImplementor) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException)

Aggregations

UnknownNamedQueryException (org.hibernate.query.UnknownNamedQueryException)3 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)2 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)2 SqmQueryImplementor (org.hibernate.query.hql.spi.SqmQueryImplementor)1 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)1 NativeQueryImplementor (org.hibernate.query.sql.spi.NativeQueryImplementor)1