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();
}
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);
}
}
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);
}
Aggregations