Search code examples
javahibernatejdbchsqldb

Trouble when using clase WHERE ... IN (list)


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:

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: .

Here the query:

Query query = session.createQuery("FROM Question q WHERE (:someTag) in (q.tagList) ORDER BY (q.creationDate) DESC").setParameter("someTag", tag).setMaxResults(amount);

And here the complete method:

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();
}

Solution

  • 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"