我目前正在使用spring mvc hibernate构建一个web应用程序。 这是我想使用EntityManager类构建的查询...
DELETE FROM registerform WHERE owner_email = 'some_user_name'
所以这里的重点是我的代码:
@PersistenceContext
EntityManager em;
@Transactional
public void removeRegisteredUser(String userName) {
CriteriaQuery<RegisterForm> c = em.getCriteriaBuilder().createQuery(RegisterForm.class);
Root<RegisterForm> from = c.from(RegisterForm.class);
c.delete(from);
c.where(em.getCriteriaBuilder().equal(from.get("owner_email"), username));
em.createQuery(c).executeUpdate();
}
所以基本上,它根本不工作,实际上c.delete
方法不存在于api ....我无法弄清楚如何在这里创建删除查询,。,
请帮助解决一些代码示例。欢呼声
答案 0 :(得分:3)
public void removeRegisteredUser(String userName) {
String hqlDelete = "delete RegisterForm where ownerEmail = :email";
int deletedEntities = entityManager.createQuery( hqlDelete )
.setParameter( "email", userName )
.executeUpdate();