Search code examples
hibernatehql

HQL with in set constraint fails


I'd like to find the aggregate of a field for ids in a list. How do you get this to work:

    final String ihql = "SELECT sum(revenue) from LineItemR where variantID in (:vid) and dtime>:dt";
    List<Long> idList = new ArrayList<>(varIds);
    return (double) Hibernate.getSessionFactory().openSession()
            .createQuery(ihql)
            .setParameter("dt", dtime)
            .setParameter("vid", idList)
            .uniqueResult();

This Fails with the following error:

INFO: could not bind value '[18992199174]' to parameter: 1;

java.util.ArrayList cannot be cast to java.lang.Long

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long


Solution

  • If you're using Hibernate instead of javax.persistence.Query, you might need to use their setParameterList() method instead of setParameter().

    See hibernate API docs for more info.