我的sudo代码是,
getCurrentSession().createQuery(myQueryBuilder.toString()).setParameter("gender", new String[] { Gender.MALE, Gender.FEMALE });
在运行时运行查询时出现错误
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
at org.hibernate.type.StringType.toString(StringType.java:44)
at org.hibernate.type.NullableType.toLoggableString(NullableType.java:218)
at org.hibernate.pretty.Printer.toString(Printer.java:76)"
我认为传递字符串数组会导致问题。
有人可以告诉我如何解决这个问题吗?
答案 0 :(得分:1)
当您调用第三方库并遇到此类错误时,您始终不得不怀疑您是否未正确使用该API。在您的情况下,从JavaDoc很容易看出正确的方法是setParameterList
而不是setParameter
。请记住,JavaDoc是你的圣经。
我正在为类Query:
复制Hibernate 3.3 Javadoc Query setParameter(int position, Object val)
Bind a value to a JDBC-style query parameter.
Query setParameter(int position, Object val, Type type)
Bind a value to a JDBC-style query parameter.
Query setParameter(String name, Object val)
Bind a value to a named query parameter.
Query setParameter(String name, Object val, Type type)
Bind a value to a named query parameter.
Query setParameterList(String name, Collection vals)
Bind multiple values to a named query parameter.
Query setParameterList(String name, Collection vals, Type type)
Bind multiple values to a named query parameter.
Query setParameterList(String name, Object[] vals)
Bind multiple values to a named query parameter.
Query setParameterList(String name, Object[] vals, Type type)
Bind multiple values to a named query parameter.
Query setParameters(Object[] values, Type[] types)
Bind values and types to positional parameters.
答案 1 :(得分:0)
1,你得到一个例外是因为你试图将一个数组String []强制转换为一个对象String,
//modify
getCurrentSession().createQuery(myQueryBuilder.toString()).setParameter("gender", Gender.MALE);
or
getCurrentSession().createQuery(myQueryBuilder.toString()).setParameter("gender", Gender.FEMALE);