1

I change Hibernate sql ,and met problem. I think its because ids is string.
How can I solve this?

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at org.hibernate.type.IntegerType.set(IntegerType.java:64)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:156)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:138)
    at org.hibernate.param.PositionalParameterSpecification.bind(PositionalParameterSpecification.java:68)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:567)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2294)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
    at org.hibernate.loader.Loader.list(Loader.java:2167)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)...

Here is my code:

public List getUser(int leader, List<Member> members) {
    String ids = String.valueOf(leader);
    if (GeneralUtil.hasValue(members)) {
        for (Member member : members) {
            ids += "," + member.getUserId(); //1,2,3,4  
        }
    }
    try {
        // Old method works well:
        // StringBuffer qryStr = new StringBuffer(
        //      "from User u where u.userId in (" + ids + ")"); 
        // return this.getHibernateTemplate().find(qryStr.toString());  

        StringBuffer qryStr = new StringBuffer("from User u where u.userId in (?)");    
        return this.getHibernateTemplate().find(qryStr.toString(), new Object[] { ids });   
    } catch (DataAccessException e) {
        e.printStackTrace();
    }
    return null;
}

1 Answer 1

1

What about Integer array?

Integer[] ids = new Integer[members.size()+1];
ids[0] = leader;
for (int i = 0; i < members.size(); i++) {
   ids[i+1] = member.getUserId();
}
1
  • But I stiil have a leader need to add to it Commented Jun 28, 2015 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.