Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 609333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:33:19+00:00 2026-05-13T17:33:19+00:00

I have a mapping (only important parts): <class name=xyz.Role table=ROLE lazy=true> <id name=id type=java.lang.Integer>

  • 0

I have a mapping (only important parts):

<class name="xyz.Role" table="ROLE" lazy="true">
  <id name="id" type="java.lang.Integer">
    <column name="ROLE_ID"/>
    <generator class="increment"/>
  </id>
  <set name="assignments" lazy="true" table="PERSON_ROLE" cascade="delete" 
inverse="true">
    <key column="ROLE_ID" />
    <many-to-many class="xyz.Person" column="PERSON_ID" />
  </set> 
</class>

and

<class name="xyz.Person" table="PERSON" lazy="true">
  <id name="Id" type="java.lang.Integer">
    <column name="TPP_ID"/>
    <generator class="increment"/>
  </id>

  <set name="roles" lazy="true" table="PERSON_ROLE" cascade="save-update">
    <key column="PERSON_ID" />
    <many-to-many class="xyz.Role" column="ROLE_ID" />
  </set> 
</class>

With this mapping, when I delete a role, very person with this role is also deleted. What I would like to achieve, is delete the association (row from PERSON_ROLE table) when I delete Role. Is there any way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:33:19+00:00Added an answer on May 13, 2026 at 5:33 pm

    Cascade works on the level of entities. Since Person_Role is not mapped as entity, cascade can not help you AFAIK.

    You might use a database-level “on cascade delete” on the foreign key from Person_Role to Role.

    Or you can – as sfussenegger points out – remove the association programatically. Note that since you mapped the association on both entities, every row in Person_Role will appear twice in your object model. In such cases, it is recommended to remove the relevant entries from both collections in order not to corrupt your object model. Hibernate however will only look at the end of the association that is not mapped with inverse="true" when persisting changes. That is, to delete from the association with your current mapping, you must delete from Person.roles, not Role.assignments:

    for (Person p : role.assignments) {
        person.roles.remove(role)
    }
    

    Or you might wish to replace the many-to-many mapping with an association entity, in which case you could simply use cascade. This would allow you to add more information to assignments more easily. For instance, if you had to express “Joe works 30% on QA and 70% as requirements engineer”, you could simply add that field to the association.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.