0

My sudo code is,

getCurrentSession().createQuery(myQueryBuilder.toString()).setParameter("gender", new String[] { Gender.MALE, Gender.FEMALE });

while running the query at runtime i get error

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

I think passing the string array is causing problem.

Can someone please tell me how to solve this?

5
  • MALE,FEMALE are public static final string in class Gender. Commented Sep 24, 2013 at 7:53
  • Don't you need to set Gender.MALE OR Gender.FEMALE INSTEAD OF both??? Commented Sep 24, 2013 at 7:56
  • 2
    What is expected to be passed to setParameter? Commented Sep 24, 2013 at 7:56
  • You are trying to compare a string to an array Commented Sep 24, 2013 at 7:56
  • You're getting an exception because you're trying to cast an array String[] to an object String. Commented Sep 24, 2013 at 9:14

2 Answers 2

2

When you call third party libraries and you get this type of errors, you always have to wonder if you are not using the API improperly. In your case it's easy to see from the JavaDoc that the correct method is setParameterList instead of setParameter . Remember, the JavaDoc is your bible.

I am taking a copy-paste of Hibernate 3.3 Javadoc for the class Query :

 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.
Sign up to request clarification or add additional context in comments.

Comments

0

1、You're getting an exception because you're trying to cast an array String[] to an object String,

//modify
getCurrentSession().createQuery(myQueryBuilder.toString()).setParameter("gender", Gender.MALE);
or
getCurrentSession().createQuery(myQueryBuilder.toString()).setParameter("gender", Gender.FEMALE);

Comments

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.