我使用序数参数和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的语法,一切似乎都没问题。 我也尝试使用命名参数,它工作。但这对我来说不是一个解决方案。我需要使用序数参数。
有人有线索吗?
答案 0 :(得分:1)
缺少选择。 查询应该是
@NamedQuery(name = "Photo.getByIds", query = "SELECT i FROM Photo i WHERE i.User.idUser = ?1 AND i.idPhoto IN ?2")