【问题标题】:Exception in creating a delete query in EntityManager在 EntityManager 中创建删除查询时出现异常
【发布时间】:2012-07-13 11:51:33
【问题描述】:

方法如下:

public void deleteVotesByReplyID(long replyId) {
        EntityManager em = getEntityManager();
        try {
           int re = em.createQuery("delete object(o) 
                                    from Vote as o 
                                    where o.memberReply.id = '"+replyId+"'"
                                  ).executeUpdate();       
        } finally {
            em.close();
        }
    }

上面的查询有什么问题? (使用 jpa 1.0)

【问题讨论】:

  • 异常堆栈跟踪说明了什么?您使用哪种 JPA 实现?
  • nothing..only java.lang.IllegalArgumentException:在 EntityManager 中创建查询时发生异常。 JPA 1.0
  • 哪个实现? Hibernate、EclipseLink、其他?
  • Toplink.......................

标签: sql jpa jpql entitymanager


【解决方案1】:

也许是因为删除查询开始了

DELETE FROM entity_name [[AS] identification_variable] [WHERE <filter>]

【讨论】:

    【解决方案2】:

    您的查询可以写成

    "DELETE from Vote o where o.memberReply.id = 'someId'"
    

    DELETE 查询也只能在活动事务中执行。你会想要

    em.getTransaction().begin();
    query.executeUpdate();
    em.getTransaction().commit();
    

    还有

    catch (Exception e) {em.getTransaction().rollback();}
    

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      相关资源
      最近更新 更多