Hibernate EntityManager,如何从table_name创建用于删除的查询,其中user_name = something

时间:2014-08-16 21:49:43

标签: sql hibernate spring-mvc entitymanager sql-delete

我目前正在使用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 ....我无法弄清楚如何在这里创建删除查询,。,

请帮助解决一些代码示例。欢呼声

1 个答案:

答案 0 :(得分:3)

public void removeRegisteredUser(String userName) { String hqlDelete = "delete RegisterForm where ownerEmail = :email"; int deletedEntities = entityManager.createQuery( hqlDelete ) .setParameter( "email", userName ) .executeUpdate();