我在使用序数参数和 IN 子句的命名查询方面遇到问题。
这是我的命名查询:
@NamedQuery(name = "Photo.getByIds", query = "FROM Photo i WHERE i.User.idUser = ?1 AND i.idPhoto IN ?2")
这是获取照片列表的代码:
TypedQuery<Photo> q = entityManager.createNamedQuery("Photo.getByIds", Photo.class);
q.setParameter(1, 50);
q.setParameter(2, Arrays.asList(50, 51));
List<Photo> photoList = q.getResultList();
这就是我不断收到的错误:
org.hibernate.QueryException: Named parameter does not appear in Query: 1 [FROM entity.Photo i WHERE i.User.idUser = ?1 AND i.idPhoto IN (:x20_, :x21_)] <java.lang.IllegalArgumentException: org.hibernate.QueryException: Named parameter does not appear in Query: 1 [FROM entity.Photo i WHERE i.User.idUser = ?1 AND i.idPhoto IN (:x20_, :x21_)]>java.lang.IllegalArgumentException: org.hibernate.QueryException: Named parameter does not appear in Query: 1 [FROM br.gov.sus.cadsus.entity.CadsusImagemFoto i WHERE i.cadsusFotografia.coSeqFotografia = ?1 AND i.coSeqImagem IN (:x20_, :x21_)]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1376)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255)
Caused by: org.hibernate.QueryException: Named parameter does not appear in Query: 1 [FROM entity.Photo i WHERE i.User.idUser = ?1 AND i.idPhoto IN (:x20_, :x21_)]
at org.hibernate.hql.classic.QueryTranslatorImpl.getNamedParameterLocs(QueryTranslatorImpl.java:551)
at org.hibernate.loader.Loader.bindNamedParameters(Loader.java:1910)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1845)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716)
at org.hibernate.loader.Loader.doQuery(Loader.java:801)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2542)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:940)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
... 25 more
怎么了?我检查了这些方法/函数/jpql 的语法,一切似乎都正常。我也尝试过使用命名参数,它奏效了。但这不是我的解决方案。我需要使用序数参数。
有人有线索吗?