Search in sources :

Example 16 with Parameter

use of javax.persistence.Parameter in project hibernate-orm by hibernate.

the class ParameterTest method testNamedParameterMetadata.

@Test
public void testNamedParameterMetadata() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<MultiTypedBasicAttributesEntity> criteria = em.getCriteriaBuilder().createQuery(MultiTypedBasicAttributesEntity.class);
    Root<MultiTypedBasicAttributesEntity> rootEntity = criteria.from(MultiTypedBasicAttributesEntity.class);
    criteria.where(em.getCriteriaBuilder().equal(rootEntity.get(MultiTypedBasicAttributesEntity_.id), em.getCriteriaBuilder().parameter(Long.class, "id")));
    TypedQuery<MultiTypedBasicAttributesEntity> query = em.createQuery(criteria);
    Parameter parameter = query.getParameter("id");
    assertEquals("id", parameter.getName());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Parameter(javax.persistence.Parameter) Test(org.junit.Test)

Example 17 with Parameter

use of javax.persistence.Parameter in project querydsl by querydsl.

the class JPAUtil method setConstants.

public static void setConstants(Query query, Map<Object, String> constants, Map<ParamExpression<?>, Object> params) {
    boolean hasParameters = !query.getParameters().isEmpty();
    for (Map.Entry<Object, String> entry : constants.entrySet()) {
        String key = entry.getValue();
        Object val = entry.getKey();
        if (Param.class.isInstance(val)) {
            val = params.get(val);
            if (val == null) {
                throw new ParamNotSetException((Param<?>) entry.getKey());
            }
        }
        if (hasParameters) {
            Parameter parameter = query.getParameter(Integer.parseInt(key));
            Class parameterType = parameter != null ? parameter.getParameterType() : null;
            if (parameterType != null && !parameterType.isInstance(val)) {
                if (val instanceof Number && Number.class.isAssignableFrom(parameterType)) {
                    val = MathUtils.cast((Number) val, parameterType);
                }
            }
        }
        query.setParameter(Integer.valueOf(key), val);
    }
}
Also used : ParamNotSetException(com.querydsl.core.types.ParamNotSetException) Parameter(javax.persistence.Parameter) Map(java.util.Map)

Example 18 with Parameter

use of javax.persistence.Parameter in project uPortal by Jasig.

the class JpaLocalAccountDaoImpl method getPeople.

@Override
public List<ILocalAccountPerson> getPeople(LocalAccountQuery query) {
    final EntityManager entityManager = this.getEntityManager();
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    final CriteriaQuery<LocalAccountPersonImpl> criteriaQuery = cb.createQuery(LocalAccountPersonImpl.class);
    final Root<LocalAccountPersonImpl> accountRoot = criteriaQuery.from(LocalAccountPersonImpl.class);
    final CollectionJoin<LocalAccountPersonImpl, LocalAccountPersonAttributeImpl> attributes = accountRoot.join(LocalAccountPersonImpl_.attributes);
    final ListJoin<LocalAccountPersonAttributeImpl, String> attributeValues = attributes.join(LocalAccountPersonAttributeImpl_.values);
    //Due to the joins multiple rows are returned for each result
    criteriaQuery.distinct(true);
    criteriaQuery.select(accountRoot);
    final List<Predicate> whereParts = new LinkedList<Predicate>();
    final Map<Parameter<String>, String> params = new LinkedHashMap<Parameter<String>, String>();
    // if a username has been specified, append it to the query
    if (query.getName() != null) {
        whereParts.add(cb.equal(accountRoot.get(LocalAccountPersonImpl_.name), this.nameParameter));
        params.put(this.nameParameter, query.getName());
    }
    //Build Predicate for each attribute being queried
    int paramCount = 0;
    for (Map.Entry<String, List<String>> entry : query.getAttributes().entrySet()) {
        final List<String> values = entry.getValue();
        if (values == null) {
            continue;
        }
        //For each value create a Predicate checking the attribute name and value together
        for (final String value : values) {
            if (StringUtils.isBlank(value)) {
                continue;
            }
            //Create Parameter objects for the name and value, stick them in the params map for later use
            final ParameterExpression<String> nameParam = this.createParameterExpression(String.class, "attrName" + paramCount);
            final ParameterExpression<String> valueParam = this.createParameterExpression(String.class, "attrValue" + paramCount);
            params.put(nameParam, entry.getKey());
            params.put(valueParam, "%" + value.toLowerCase() + "%");
            //Build the and(eq, like) predicate and add it to the list of predicates for the where clause
            whereParts.add(cb.and(cb.equal(attributes.get(LocalAccountPersonAttributeImpl_.name), nameParam), cb.like(cb.lower(attributeValues.as(String.class)), valueParam)));
            paramCount++;
        }
    }
    //Add the Predicates to the where clause
    criteriaQuery.where(cb.or(whereParts.toArray(new Predicate[whereParts.size()])));
    //Create the query
    final TypedQuery<LocalAccountPersonImpl> jpaQuery = this.createCachedQuery(criteriaQuery);
    //Add all of the stored up parameters to the query
    for (Map.Entry<Parameter<String>, String> entry : params.entrySet()) {
        final Parameter<String> parameter = entry.getKey();
        final String value = entry.getValue();
        jpaQuery.setParameter(parameter, value);
    }
    final List<LocalAccountPersonImpl> accounts = jpaQuery.getResultList();
    return new ArrayList<ILocalAccountPerson>(accounts);
}
Also used : ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LinkedList(java.util.LinkedList) EntityManager(javax.persistence.EntityManager) Parameter(javax.persistence.Parameter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Parameter (javax.persistence.Parameter)18 EntityManager (javax.persistence.EntityManager)16 Test (org.junit.Test)15 List (java.util.List)9 Query (javax.persistence.Query)9 ArrayList (java.util.ArrayList)7 Item (org.hibernate.jpa.test.Item)6 SkipForDialect (org.hibernate.testing.SkipForDialect)6 StoredProcedureQuery (javax.persistence.StoredProcedureQuery)5 TestForIssue (org.hibernate.testing.TestForIssue)5 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)3 NoResultException (javax.persistence.NoResultException)3 PersistenceException (javax.persistence.PersistenceException)3 StoredProcedureParameter (javax.persistence.StoredProcedureParameter)3 Map (java.util.Map)2 Wallet (org.hibernate.jpa.test.Wallet)2 ParamNotSetException (com.querydsl.core.types.ParamNotSetException)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 LinkedHashMap (java.util.LinkedHashMap)1