我在Schedule和Agent之间存在多对多关系(通过属性assignedAgent)。
我想查找包含我提供的任何代理的计划表。我想这样做:
List<Agent> agentsToMatch = // ... I want schedules with any of these agents
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Schedule> query = cb.createQuery(Schedule.class);
Root<Schedule> schedule = query.from(Schedule.class);
query.where(schedule.get(Schedule_.assignedAgents).in( agentsToMatch ));
使用Hibernate 5.2.7,在数组中提供2个代理时会得到以下查询:
select generatedAlias0 from Schedule as generatedAlias0 where generatedAlias0.assignedAgents in (:param0, :param1)
然而,似乎没有设置参数,因为我得到以下异常:
WARN [qtp1782580546-44] o.h.engine.jdbc.spi.SqlExceptionHelper:129 - SQL Error: 0, SQLState: 22023
ERROR [qtp1782580546-44] o.h.engine.jdbc.spi.SqlExceptionHelper:131 - No value specified for parameter 1.
javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not extract ResultSet
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1419)
at org.hibernate.Query.getResultList(Query.java:427)
at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:72)
我可能会出错的任何想法?
答案 0 :(得分:1)
我通过用.join替换.get来修复问题: 之前:
query.where(schedule.get(Schedule_.assignedAgents).in( agentsToMatch ));
后:
query.where(schedule.join(Schedule_.assignedAgents).in( agentsToMatch ));
答案 1 :(得分:0)
正确的方法应该是
query.setParameter("agentsToMatch", agentsToMatch);
然后为查询提供参数
{{1}}