How to delete child records, and not set the foreign key to null

I have a user and a set of authority in a one-to-many relationship:

User.hbm.xml:

<set name="authorities" table="authorities" cascade="all-delete-orphan">
    <key column="user_id" />
    <one-to-many class="com.ebisent.domain.Authority" />
</set>

When I delete a user, I also want to remove the permissions, but what happens is that the foreign key of the child table (authority.user_id) is null. Then I get the following error, and deleting the user is canceled:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

Updating .user_id privileges to null is not a rollback.

How can I remove privileges when deleting a parent user?

EDIT: I got this working by explicitly deleting privileges by calling refresh()on the user and then deleting the user, but I would like to know the โ€œrightโ€ way to do this.

+3
1

, all-delete-orphan delete . :

Parent p = (Parent) session.load(Parent.class, pid);
session.delete(p);
session.flush();

all,delete-orphan delete ( ). ? , ?

, , delete-orphan delete, ... session.flush(), .

. . , :

<set name="authorities" table="authorities" cascade="all-delete-orphan">
    <key column="user_id" not-null="true"/>
    <one-to-many class="com.ebisent.domain.Authority" />
</set>

.

+1

Source: https://habr.com/ru/post/1765511/


All Articles