I have a method in Dao Class that returns List<Object[]> back and I am using named Query

public List<Object[]> getListByCustomer(Session session, int customerId, List<Integer> strIds) {
  Query namedQuery = session.createSQLQuery(QueryConstants.EXPORT);
  namedQuery.setParameter("customer", customerId);
  namedQuery.setParameter("stringId", strIds);
  List<Object[]> objects = namedQuery.list();
  return objects;
}

I want to pass List<Integer> strIds in stringId into the named query as follows :

public class QueryConstants {
  public static final String EXPORT = 
    "SELECT sv.NAME, sv.TYPE, sv.CLIENT_ADDRESS, sv.NAME_REDUNDANT, sv.DEPARTURE_DATE, s1.CODE,sv.STATE, sv.CODE "
    + "FROM VIEW sv, PROCESS p1, SET s1 " 
    + "WHERE sv.R_ID = p1.R_ID and p1.ISSUER_ID = s1.USER_ID and sv.CUSTOMER_ID = :customer and sv.R_ID IN (:stringId)";
}

But I get ORA-00932: inconsistent datatypes: expected NUMBER got BINARY.

Also when I remove sv.R_ID IN (:stringId) from the query it works fine and when I pass Integer(strIds) instead of List<Integer> strIds into the query it works fine.

I'm using Oracle 10g.

有帮助吗?

解决方案 2

I think you just need to use

 IN :stringId

instead of

 IN (:stringId)

For JPA

namedQuery.setParameter("stringId", strIds);

is correct, but for Hibernate you should use

namedQuery.setParameterList("stringId", strIds);

其他提示

This is a very misleading error, and may root from different causes, for me I was setting a parameter that it was supposedly a number but at runtime it was setting null, hence it was binary. On a separate occasion got this error due to bean creation error in spring and was not setting the parameter correctly as well.

I encountered this same exception and found the below reason for that -

In my entity, a field was mapped to a custom object (Parent child relationship - @ManyToOne). Later, the relationship annotation was removed by developer but the datatype was not changed.

After removing the @ManyToOne annotation, the @Column annotation should have been used with appropriate data type (Integer).

Case your param is list. If list is empty then raise error, you must check that list not empty to avoid error. Case your param is single value. Let use TO_NUMBER(:your_param) to avoid error. It working on me.

In my case, I was using HQL (in the repository of spring data) with an Entity mapped with @Enumerated (ORDINAL). I was trying to use the the enum object directly in the where clausule. The solution was to use TO_NUMBER(:your_param) as mentioned by the member above.

I got the same error but for a different reason. In my case it was due to the order the parameters were supplied being different from the order defined in the query. I assumed (wrongly) that because the parameters were named the order didn't matter. Sadly, it seems like it does.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top