执行EntityManageer时出错使用in子句参数删除本机查询

时间:2013-10-13 00:50:11

标签: java hibernate persistence

使用in子句参数执行EntityManageer删除本机查询时出错。该参数的类型为List,我想做类似的事情 -

List<UUID> ids = ...
entityManager.createNativeQuery("DELETE FROM FOO WHERE ID IN (:ids)")
        .setParameter("ids", ids)
        .executeUpdate();

我得到的例外是 -

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: ERROR: IN could not convert type bytea to uuid
  Position: 39
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:108)
[snip]

请告诉我如何运行上面的删除查询。

1 个答案:

答案 0 :(得分:0)

由于它是Native查询,您应该具有以下内容:entityManager.createNativeQuery("DELETE FROM FOO WHERE ID IN (?1)") .setParameter(1, ids) .executeUpdate();

此外,我认为在这种情况下您不需要本机查询,下面的代码也应该有效:

entityManager.createQuery("DELETE FROM FOO WHERE ID IN (:ids)")
    .setParameter("ids", ids)
    .executeUpdate();