我有一个product
表,如下所示;
字段如下;
如您所见,除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='?'")