Hello. I'm trying to search list of (entities) messages with paging. Could you tell me what i'm doing wrong in the first case?
// Fails with error: java.lang.IllegalArgumentException: Parameter value [org.hibernate.jpa.criteria.expression.ParameterExpressionImpl@5177c65e] did not match expected type [...MsgType(n/a)] at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:874) at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:80) at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:248) at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:620) at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:180)
//Main query CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class); Root<Message> from = criteriaQuery.from(Message.class); CriteriaQuery<Message> select = criteriaQuery.select(from);
//Subquery Subquery<Message> subquery = criteriaQuery.subquery(Message.class); Root<Message> fromSimpleBean = subquery.from(Message.class); subquery.select(fromSimpleBean.<Message>get("id"));
Parameter<MsgType> param1 = criteriaBuilder.parameter(MsgType.class, "msgType"); Parameter<MessageDirection> param2 = criteriaBuilder.parameter(MessageDirection.class, "direction");
Predicate where = criteriaBuilder.and( criteriaBuilder.equal(fromSimpleBean.get("mesageType"), param1), criteriaBuilder.equal(fromSimpleBean.get("direction"), param2)); subquery.where(where);
select.where(criteriaBuilder.in(from.get("id")).value(subquery));
TypedQuery<Message> typedQuery = em.createQuery(select).setParameter("direction", MessageDirection.I).setParameter("msgType", MsgType.SMS);
//Works fine: //Main query CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class); Root<Message> from = criteriaQuery.from(Message.class); CriteriaQuery<Message> select = criteriaQuery.select(from);
//Subquery Subquery<Message> subquery = criteriaQuery.subquery(Message.class); Root<Message> fromSimpleBean = subquery.from(Message.class); subquery.select(fromSimpleBean.<Message>get("id"));
Predicate where = criteriaBuilder.and( criteriaBuilder.equal(fromSimpleBean.get("mesageType"), criteriaBuilder.parameter(MsgType.class, "msgType")), criteriaBuilder.equal(fromSimpleBean.get("direction"), criteriaBuilder.parameter(MessageDirection.class, "direction"))); subquery.where(where);
select.where(criteriaBuilder.in(from.get("id")).value(subquery));
TypedQuery<Message> typedQuery = em.createQuery(select).setParameter("direction", MessageDirection.I).setParameter("msgType", MsgType.SMS);
|