# Hibernate cannot delete

**URL:** <https://discourse.hibernate.org/t/hibernate-cannot-delete/736>\
**Category:** Hibernate ORM\
**Created:** [May 10, 2018, 11:37am UTC](https://discourse.hibernate.org/t/hibernate-cannot-delete/736 "2018-05-10T11:37:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bellamabel2018](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bellamabel2018](https://discourse.hibernate.org/u/bellamabel2018)\
**Post date:** [May 10, 2018, 11:37am UTC](https://discourse.hibernate.org/t/hibernate-cannot-delete/736/1 "2018-05-10T11:37:00Z")

</div>

I’m using hibernate.

@Entity  
@Table(name=“user”)  
public class User{  
@ManyToMany(fetch = FetchType.LAZY)  
@JoinTable(name = “user\_roles”, joinColumns = @JoinColumn(name = “user\_id”, referencedColumnName = “id”), inverseJoinColumns = @JoinColumn(name = “role\_id”, referencedColumnName = “id”))  
private List roles;  
}

@Entity  
@Table(name=“role”)  
public class Role {  
@ManyToMany(mappedBy = “roles”, fetch = FetchType.LAZY)  
private List users;  
}

through repository.delete Role  
Cannot delete the association table relationship.Exception:  
Cannot delete or update a parent row: a foreign key constraint fails

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 10, 2018, 12:00pm UTC](https://discourse.hibernate.org/t/hibernate-cannot-delete/736/2 "2018-05-10T12:00:09Z")

</div>

Prior to deleting a Role, you have to remove it from all Users.

So, iterate role.users and remove the role from each User.roles.

Or, execute a native SQL:

```
delete from user_roles where role_id = :roleId
```
