JPA删除查询不起作用

问题描述 投票:1回答:3

好吧,我不明白为什么我的代码不起作用。有人可以看看。它不提供任何错误消息,但不会删除客户。其他方法运行良好(getCustomerbyId,getAllCustomers等)谢谢

public void deleteCustomerById(long id) {
        EntityManager em = null;
        try {
            em = JpaUtil.getFactory().createEntityManager();
            em.getTransaction().begin();
            Query query = em.createQuery("Delete  from Customer c where c.id = :id");
            query.setParameter("id", id);
            em.getTransaction().commit();
        } finally {
            JpaUtil.closeQuietly(em);
        }
    }
java sql jpa entitymanager
3个回答
2
投票

您正在创建查询但不执行它。你应该添加

query.executeUpdate();

在提交之前


2
投票

您需要执行查询以将SQL问题发送到数据库;在这种情况下,您将需要使用executeUpdate()并获取修改后的行计数以验证是否已删除某些内容。

em.getTransaction().begin();
Query query = em.createQuery("Delete  from Customer c where c.id = :id");
query.setParameter("id", id);
int rows = query.executeUpdate();
em.getTransaction().commit();

0
投票

使用jpa APis find(Customer.class)查找客户对象然后使用romove(对象)

© www.soinside.com 2019 - 2024. All rights reserved.