JPA TypedQuery: Parameter value element did not match expected type

I am using JPA 2.0 and having following code in DAO layer:

public void test() {
  String key = "status";
  String[] statuses = {"A", "B"};
  List<TestTable> result = find(key, statuses);
}

public List<TestTable> find(String key, Object valueArr) {
  String sql = "SELECT nd FROM TestTable nd WHERE nd." + key + " IN :" + key;
  TypedQuery<TestTable> query = entityManager.createQuery(sql, TestTable.class);
  query.setParameter(key, Arrays.asList(valueArr))//***Error Line***
  return query.getResultList();
}

In the above Error Line it throws following exception:

java.lang.IllegalArgumentException: Parameter value element [[Ljava.lang.String;@cbe5bc] did not match expected type [java.lang.String]

Why it expected type is String whereas actually it is String[] ? Please help!

Note: This is the extracted and simplified code from the generic routine I cannot change the 'Object valueArr' to String[] because its being used for others...

Jon Skeet
people
quotationmark

You've got a variable of type Object, and you're calling Arrays.asList. That means you've effectively called:

List<Object> values = Arrays.asList(new Object[] {valueArr});

So you're creating a list of a single element, where that element is an array. That's not what you want... you want a List<Object> with as many elements as you had to start with. The simplest way to do that is to change your parameter type:

public List<TestTable> find(String key, Object[] values)

...

query.setParameter(key, Arrays.asList(values));

people

See more on this question at Stackoverflow