错误:运算符不存在:整数=字符变化(即使一切都是整数)

时间:2015-04-07 08:55:08

标签: sql postgresql jpa

我有一个product表,如下所示;

enter image description here

字段如下;

enter image description here

如您所见,除id外,order_id为整数,其余为变量。

当我执行以下查询时,它给出了我无法在order_id字段中比较整数和varchar的错误。

private final static String removeAlternativeProductQuery = "DELETE FROM product where order_id=? AND product_id='?' AND alternative_product_id='?'";

public void removeAlternativeProduct(AlternativeProduct alternativeProduct){

    Query query = em.createNativeQuery(removeAlternativeProductQuery);
    query.setParameter(1, (Integer) alternativeProduct.getOrderId());
    query.setParameter(2, alternativeProduct.getProductId());
    query.setParameter(3, alternativeProduct.getAlternativeProductId());
    query.executeUpdate();

}

与数据库并行,orderId是Integer,其余的是我对象中的String。那我为什么会得到这个演员错误?

为了更清楚,在查询where order_id=?中,左侧是整数。右侧取自

query.setParameter(1, (Integer) alternativeProduct.getOrderId());

也是一个整数。那么这里的字符变化错误在哪里?

完整的错误;

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 54
Error Code: 0
Call: DELETE FROM product where order_id=? AND product_id='?' AND alternative_product_id='?'
    bind => [1 parameter bound]
Query: DataReadQuery(sql="DELETE FROM product where order_id=? AND product_id='?' AND alternative_product_id='?'")

0 个答案:

没有答案