Friday, December 10, 2010

Hibernate setParameterList nudge the data type

OK, I've run into this before so I thought I would write it down this time. You do a sql query for a list of ids and they come back as big integers, but really in your object they are longs. So when you go to use the list as a parameter it will complain. However, if you do setParameterList("ids", ids, Hibernate.BIG_INTEGER) it doesn't complain. Sort of counter intuitive to me because your object id is a long. But I guess what you are telling it is what the stuff in the param list is? Whatever, it works. 

Collection<Long> ids = new ArrayList<Long>();

if (!frm.getRygs().isEmpty()) {
    Collection<Long> rygIds = reportDao
            .getSession()
            .createSQLQuery(
                    "select u.id from av_user "
                            + " u left join av_contribution c on u.contribid = c.id "
                            + " where c.redyellowgreen in (:rygs);")
            .setParameterList("rygs", toStringList(frm.getRygs()))
            .list();
    ids.addAll(rygIds);
}

if (!ids.isEmpty()) {
    List<User> users = reportDao.getSession().createQuery(
            "from User where id in (:ids) order by lastName")
            .setParameterList("ids", ids, Hibernate.BIG_INTEGER).list();

No comments:

Post a Comment