使用clase WHERE ... IN(清单)时出现问题 [英] Trouble when using clase WHERE ... IN (list)
问题描述
尝试使用Hibernate从数据库中检索时遇到麻烦.我想做的是从数据库中检索问题(使用Hibernate和HSQLDB),其中问题列表中包含标签tag
.这是错误:
I'm having trouble when trying to retrieve from database using Hibernate. What I'm trying to do is to retrieve questions from the database (using Hibernate with HSQLDB) where the Tag tag
is contained in the question list. Here is the error:
2017年1月6日19:43:26.021警告[http-apr-8080-exec-4] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL错误:-5585,SQLState:42585 06-Jan -2017 19:43:26.021错误[http-apr-8080-exec-4] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions格式错误的数字常量:.
06-Jan-2017 19:43:26.021 WARN [http-apr-8080-exec-4] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: -5585, SQLState: 42585 06-Jan-2017 19:43:26.021 ERROR [http-apr-8080-exec-4] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions malformed numeric constant: .
此处查询:
Query query = session.createQuery("FROM Question q WHERE (:someTag) in (q.tagList) ORDER BY (q.creationDate) DESC").setParameter("someTag", tag).setMaxResults(amount);
这里是完整的方法:
public static List<Question> list(Tag tag, int amount){
Session session = HibernateUtil.getSession();
Query query = session.createQuery("FROM Question q WHERE (:someTag) in (q.tagList) ORDER BY (q.creationDate) DESC").setParameter("someTag", tag).setMaxResults(amount);
return query.list();
}
推荐答案
如果要检查列表是否包含元素,则需要使用elements
子句,在这种情况下,查询如下所示:
You need to use elements
clause if you are trying to check whether a list contains an element, in this case, the query would look like this:
"FROM Question q WHERE :someTag in elements(q.tagList) ORDER BY (q.creationDate) DESC"
这篇关于使用clase WHERE ... IN(清单)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!