Search in sources :

Example 1 with ParameterExpression

use of javax.persistence.criteria.ParameterExpression in project hibernate-orm by hibernate.

the class QueryStructure method getParameters.

// PARAMETERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public Set<ParameterExpression<?>> getParameters() {
    final Set<ParameterExpression<?>> parameters = new LinkedHashSet<ParameterExpression<?>>();
    final ParameterRegistry registry = new ParameterRegistry() {

        public void registerParameter(ParameterExpression<?> parameter) {
            parameters.add(parameter);
        }
    };
    ParameterContainer.Helper.possibleParameter(selection, registry);
    ParameterContainer.Helper.possibleParameter(restriction, registry);
    ParameterContainer.Helper.possibleParameter(having, registry);
    if (subqueries != null) {
        for (Subquery subquery : subqueries) {
            ParameterContainer.Helper.possibleParameter(subquery, registry);
        }
    }
    // both group-by and having expressions can (though unlikely) contain parameters...
    ParameterContainer.Helper.possibleParameter(having, registry);
    if (groupings != null) {
        for (Expression<?> grouping : groupings) {
            ParameterContainer.Helper.possibleParameter(grouping, registry);
        }
    }
    return parameters;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ParameterExpression(javax.persistence.criteria.ParameterExpression) Subquery(javax.persistence.criteria.Subquery)

Example 2 with ParameterExpression

use of javax.persistence.criteria.ParameterExpression in project hibernate-orm by hibernate.

the class CriteriaCompiler method compile.

public QueryImplementor compile(CompilableCriteria criteria) {
    try {
        criteria.validate();
    } catch (IllegalStateException ise) {
        throw new IllegalArgumentException("Error occurred validating the Criteria", ise);
    }
    final Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap = new HashMap<>();
    final List<ImplicitParameterBinding> implicitParameterBindings = new ArrayList<>();
    RenderingContext renderingContext = new RenderingContext() {

        private int aliasCount;

        private int explicitParameterCount;

        public String generateAlias() {
            return "generatedAlias" + aliasCount++;
        }

        public String generateParameterName() {
            return "param" + explicitParameterCount++;
        }

        @Override
        @SuppressWarnings("unchecked")
        public ExplicitParameterInfo registerExplicitParameter(ParameterExpression<?> criteriaQueryParameter) {
            ExplicitParameterInfo parameterInfo = explicitParameterInfoMap.get(criteriaQueryParameter);
            if (parameterInfo == null) {
                if (StringHelper.isNotEmpty(criteriaQueryParameter.getName())) {
                    parameterInfo = new ExplicitParameterInfo(criteriaQueryParameter.getName(), null, criteriaQueryParameter.getJavaType());
                } else if (criteriaQueryParameter.getPosition() != null) {
                    parameterInfo = new ExplicitParameterInfo(null, criteriaQueryParameter.getPosition(), criteriaQueryParameter.getJavaType());
                } else {
                    parameterInfo = new ExplicitParameterInfo(generateParameterName(), null, criteriaQueryParameter.getJavaType());
                }
                explicitParameterInfoMap.put(criteriaQueryParameter, parameterInfo);
            }
            return parameterInfo;
        }

        public String registerLiteralParameterBinding(final Object literal, final Class javaType) {
            final String parameterName = generateParameterName();
            final ImplicitParameterBinding binding = new ImplicitParameterBinding() {

                public String getParameterName() {
                    return parameterName;
                }

                public Class getJavaType() {
                    return javaType;
                }

                public void bind(TypedQuery typedQuery) {
                    typedQuery.setParameter(parameterName, literal);
                }
            };
            implicitParameterBindings.add(binding);
            return parameterName;
        }

        public String getCastType(Class javaType) {
            SessionFactoryImplementor factory = entityManager.getFactory();
            Type hibernateType = factory.getTypeResolver().heuristicType(javaType.getName());
            if (hibernateType == null) {
                throw new IllegalArgumentException("Could not convert java type [" + javaType.getName() + "] to Hibernate type");
            }
            return hibernateType.getName();
        }
    };
    return criteria.interpret(renderingContext).buildCompiledQuery(entityManager, new InterpretedParameterMetadata() {

        @Override
        public Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap() {
            return explicitParameterInfoMap;
        }

        @Override
        public List<ImplicitParameterBinding> implicitParameterBindings() {
            return implicitParameterBindings;
        }
    });
}
Also used : HashMap(java.util.HashMap) TypedQuery(javax.persistence.TypedQuery) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ArrayList(java.util.ArrayList) Type(org.hibernate.type.Type) ParameterExpression(javax.persistence.criteria.ParameterExpression) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ParameterExpression

use of javax.persistence.criteria.ParameterExpression in project hibernate-orm by hibernate.

the class BasicCriteriaUsageTest method testParameterCollection.

@Test
public void testParameterCollection() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Wall> criteria = em.getCriteriaBuilder().createQuery(Wall.class);
    Root<Wall> from = criteria.from(Wall.class);
    ParameterExpression param = em.getCriteriaBuilder().parameter(String.class);
    SingularAttribute<? super Wall, ?> colorAttribute = em.getMetamodel().entity(Wall.class).getDeclaredSingularAttribute("color");
    assertNotNull("metamodel returned null singular attribute", colorAttribute);
    Predicate predicate = em.getCriteriaBuilder().equal(from.get(colorAttribute), param);
    criteria.where(predicate);
    assertEquals(1, criteria.getParameters().size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) ParameterExpression(javax.persistence.criteria.ParameterExpression) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Aggregations

ParameterExpression (javax.persistence.criteria.ParameterExpression)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 EntityManager (javax.persistence.EntityManager)1 TypedQuery (javax.persistence.TypedQuery)1 Predicate (javax.persistence.criteria.Predicate)1 Subquery (javax.persistence.criteria.Subquery)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 Type (org.hibernate.type.Type)1 Test (org.junit.Test)1