【问题标题】:How to remove some rows in table via EntityManager? [closed]如何通过 EntityManager 删除表中的某些行? [关闭]
【发布时间】:2013-06-09 03:28:39
【问题描述】:

例如在 sql 查询中会:

delete from Mails where name='....'

如何通过 EntityManager 执行?

【问题讨论】:

  • entityManager.remove(name)
  • 它不工作。我试过了:EntityManager em = Server.emf.createEntityManager(); em.getTransaction().begin();邮件 mail = new Mails(); mail.setName(用户名); em.remove(邮件); em.getTransaction().commit(); em.close();

标签: java sql jpa entitymanager


【解决方案1】:

您可以使用命名查询来做到这一点。 Here's an example:

query = em.createNamedQuery("SELECT c FROM Country c");
List results = query.getResultList();

不要使用选择,而是使用您想要的方式删除。注意这里不使用SQL,而是JPQL

【讨论】:

  • em.createQuery().executeQuery 解决了我的问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-01-13
  • 2020-09-23
  • 1970-01-01
  • 2012-11-03
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多