Search in sources :

Example 1 with InterpretedParameterMetadata

use of org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata 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 2 with InterpretedParameterMetadata

use of org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata in project hibernate-orm by hibernate.

the class CriteriaQueryImpl method interpret.

@Override
public CriteriaInterpretation interpret(RenderingContext renderingContext) {
    final StringBuilder jpaqlBuffer = new StringBuilder();
    queryStructure.render(jpaqlBuffer, renderingContext);
    if (!getOrderList().isEmpty()) {
        jpaqlBuffer.append(" order by ");
        String sep = "";
        for (Order orderSpec : getOrderList()) {
            jpaqlBuffer.append(sep).append(((Renderable) orderSpec.getExpression()).render(renderingContext)).append(orderSpec.isAscending() ? " asc" : " desc");
            sep = ", ";
        }
    }
    final String jpaqlString = jpaqlBuffer.toString();
    log.debugf("Rendered criteria query -> %s", jpaqlString);
    return new CriteriaInterpretation() {

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

                @Override
                public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
                    SelectionImplementor selection = (SelectionImplementor) queryStructure.getSelection();
                    return selection == null ? null : selection.getValueHandlers();
                }

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

                @Override
                public ResultMetadataValidator getResultMetadataValidator() {
                    return new HibernateEntityManagerImplementor.QueryOptions.ResultMetadataValidator() {

                        @Override
                        public void validate(Type[] returnTypes) {
                            SelectionImplementor selection = (SelectionImplementor) queryStructure.getSelection();
                            if (selection != null) {
                                if (selection.isCompoundSelection()) {
                                    if (returnTypes.length != selection.getCompoundSelectionItems().size()) {
                                        throw new IllegalStateException("Number of return values [" + returnTypes.length + "] did not match expected [" + selection.getCompoundSelectionItems().size() + "]");
                                    }
                                } else {
                                    if (returnTypes.length > 1) {
                                        throw new IllegalStateException("Number of return values [" + returnTypes.length + "] did not match expected [1]");
                                    }
                                }
                            }
                        }
                    };
                }
            });
            for (ImplicitParameterBinding implicitParameterBinding : parameterMetadata.implicitParameterBindings()) {
                implicitParameterBinding.bind(jpaqlQuery);
            }
            return new CriteriaQueryTypeQueryAdapter(entityManager, jpaqlQuery, parameterMetadata.explicitParameterInfoMap());
        }

        private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {
            final HashMap<String, Class> map = new HashMap<String, Class>();
            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) List(java.util.List) Order(javax.persistence.criteria.Order) InterpretedParameterMetadata(org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata) EntityType(javax.persistence.metamodel.EntityType) Type(org.hibernate.type.Type) CriteriaQueryTypeQueryAdapter(org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) HashMap(java.util.HashMap) Map(java.util.Map) ImplicitParameterBinding(org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding)

Aggregations

HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 HibernateEntityManagerImplementor (org.hibernate.jpa.spi.HibernateEntityManagerImplementor)2 CriteriaInterpretation (org.hibernate.query.criteria.internal.compile.CriteriaInterpretation)2 ImplicitParameterBinding (org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding)2 InterpretedParameterMetadata (org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata)2 Order (javax.persistence.criteria.Order)1 EntityType (javax.persistence.metamodel.EntityType)1 CriteriaQueryTypeQueryAdapter (org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter)1 QueryImplementor (org.hibernate.query.spi.QueryImplementor)1 Type (org.hibernate.type.Type)1