Search in sources :

Example 1 with QueryImplementor

use of org.hibernate.query.spi.QueryImplementor in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method buildQueryFromName.

protected <T> QueryImplementor<T> buildQueryFromName(String name, Class<T> resultType) {
    checkOpen();
    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 + "]"));
}
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 2 with QueryImplementor

use of org.hibernate.query.spi.QueryImplementor in project hibernate-orm by hibernate.

the class AbstractManipulationCriteriaQuery method interpret.

@Override
public CriteriaInterpretation interpret(RenderingContext renderingContext) {
    final String jpaqlString = renderQuery(renderingContext);
    return new CriteriaInterpretation() {

        @Override
        @SuppressWarnings("unchecked")
        public QueryImplementor buildCompiledQuery(SessionImplementor entityManager, final InterpretedParameterMetadata interpretedParameterMetadata) {
            final Map<String, Class> implicitParameterTypes = extractTypeMap(interpretedParameterMetadata.implicitParameterBindings());
            QueryImplementor query = entityManager.createQuery(jpaqlString, null, null, new HibernateEntityManagerImplementor.QueryOptions() {

                @Override
                public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
                    return null;
                }

                @Override
                public Map<String, Class> getNamedParameterExplicitTypes() {
                    return implicitParameterTypes;
                }

                @Override
                public ResultMetadataValidator getResultMetadataValidator() {
                    return null;
                }
            });
            for (ImplicitParameterBinding implicitParameterBinding : interpretedParameterMetadata.implicitParameterBindings()) {
                implicitParameterBinding.bind(query);
            }
            return query;
        }

        private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {
            final HashMap<String, Class> map = new HashMap<>();
            for (ImplicitParameterBinding implicitParameter : implicitParameterBindings) {
                map.put(implicitParameter.getParameterName(), implicitParameter.getJavaType());
            }
            return map;
        }
    };
}
Also used : HashMap(java.util.HashMap) CriteriaInterpretation(org.hibernate.query.criteria.internal.compile.CriteriaInterpretation) HibernateEntityManagerImplementor(org.hibernate.jpa.spi.HibernateEntityManagerImplementor) InterpretedParameterMetadata(org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) List(java.util.List) QueryImplementor(org.hibernate.query.spi.QueryImplementor) HashMap(java.util.HashMap) Map(java.util.Map) ImplicitParameterBinding(org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding)

Example 3 with QueryImplementor

use of org.hibernate.query.spi.QueryImplementor in project hibernate-orm by hibernate.

the class SessionImpl method createQuery.

@Override
@SuppressWarnings("unchecked")
public <T> QueryImplementor<T> createQuery(String jpaqlString, Class<T> resultClass, Selection selection, QueryOptions queryOptions) {
    try {
        final QueryImplementor query = createQuery(jpaqlString);
        if (queryOptions.getValueHandlers() == null) {
            if (queryOptions.getResultMetadataValidator() != null) {
                queryOptions.getResultMetadataValidator().validate(query.getReturnTypes());
            }
        }
        // determine if we need a result transformer
        List tupleElements = Tuple.class.equals(resultClass) ? ((CompoundSelectionImpl<Tuple>) selection).getCompoundSelectionItems() : null;
        if (queryOptions.getValueHandlers() != null || tupleElements != null) {
            query.setResultTransformer(new CriteriaQueryTupleTransformer(queryOptions.getValueHandlers(), tupleElements));
        }
        return query;
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) QueryImplementor(org.hibernate.query.spi.QueryImplementor) Tuple(javax.persistence.Tuple) CriteriaQueryTupleTransformer(org.hibernate.jpa.spi.CriteriaQueryTupleTransformer)

Aggregations

QueryImplementor (org.hibernate.query.spi.QueryImplementor)3 List (java.util.List)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Tuple (javax.persistence.Tuple)1 NamedQueryDefinition (org.hibernate.engine.spi.NamedQueryDefinition)1 NamedSQLQueryDefinition (org.hibernate.engine.spi.NamedSQLQueryDefinition)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 CriteriaQueryTupleTransformer (org.hibernate.jpa.spi.CriteriaQueryTupleTransformer)1 HibernateEntityManagerImplementor (org.hibernate.jpa.spi.HibernateEntityManagerImplementor)1 CriteriaInterpretation (org.hibernate.query.criteria.internal.compile.CriteriaInterpretation)1 ImplicitParameterBinding (org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding)1 InterpretedParameterMetadata (org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata)1 NativeQueryImplementor (org.hibernate.query.spi.NativeQueryImplementor)1