0

In Bean class there is this annotation:

   class User {  
        @OneToMany(targetEntity = Feedback.class,mappedBy = "user",cascade =   
        CascadeType.ALL,fetch = FetchType.LAZY)  
    private List<Feedback> feedbacks;  
        ...  
    }

I want to delete user, which has some feedbacks. If I type this:

sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().get(User.class, id));

user will be deleted successfully, (id is primary key). But I want to delete all users, witch, have role="admin", and if i type this query:

String query = "DELETE from User WHERE role='" + role + "'";
    sessionFactory.getCurrentSession().createQuery(query).executeUpdate();

It will be delete only users without references to feedbacks. What wrong with my query? Please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yurii
  • 1

1 Answers1

1

Nothing is wrong with your query. HQL delete queries do not "touch" associations. That is per JPA spec.

Check out this issue in Hibernate Jira about adding support for adding a new keyword CASCADE to delete queries for specifying associations which should also be deleted.

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46