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

  • SEARCH
  • Home
  • 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 450755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:54:02+00:00 2026-05-12T21:54:02+00:00

I have two classes A and B. Many B’s can have association with a

  • 0

I have two classes A and B. Many B’s can have association with a single A, hence a many-to-one relationship from B to A. I’ve mapped the relationship like:

<class name="A" table="tbl_A">
  <property name="propA" column="colA"/>
</class>
<class name="B" table="tbl_B">
  <property name="propB" column="colB"/>
  <many-to-one name="a" class="A" column="col1" cascade="delete"/>
</class>

A has nothing mapped to B. Keeping this in mind we intend to delete B when it’s associated A is deleted. This could have been possible if I could define an inverse=”true” on the many-to-one association in B but hibernate does not allow that.

Can anyone help with this? We do not want to write anything in A for this.

  • 1 1 Answer
  • 1 View
  • 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-12T21:54:02+00:00Added an answer on May 12, 2026 at 9:54 pm

    Hibernate only cascades along the defined associations. If A knows nothing about Bs, nothing you do with A will affect Bs.

    Pascal’s suggestion is, therefore, the easiest way to do what you want:

    <class name="A" table="tbl_A">
      ...
      <set name="myBs" inverse="true" cascade="all,delete-orphan">
        <key column="col1"/>
        <one-to-many class="B"/>
      </set>
    </class>
    
    <class name="B" table="tbl_B">
      ...
      <many-to-one name="a" class="A" column="col1" not-null="true"/>
    </class>
    

    Note that setting cascade="delete" on B as you have it in your original code will NOT do what you want – it tells Hibernate to “delete A if B is deleted” which is likely to result in constraint violation (if there are any other Bs linked to that A).

    If you absolutely cannot add a collection of Bs to A (though I can’t really think of the circumstances where that’d be the case), your only other alternative is to define cascade delete from A to B at the foreign key level; your Bs will then be deleted when your A is deleted.

    This is a rather ugly solution, however, because you have to be extremely careful of how you delete A in Hibernate:

    1. Session must be flushed prior to deleting A (having pending updates to B may result in an error or A and some Bs being re-inserted behind the scenes)
    2. All Bs linked to your A (and since you’re not maintaining the relationship from A side that means all Bs) must be evicted from all active sessions and 2nd level cache.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: Parent and Child , mapped one-to-many with relationship owned by
I have a one-to-many relationship between two classes for this situation. I have a
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes, one that inherits from the other. The base class is
I have two classes in a many-to-many relationship, but only one of them have
I have two classes university and department, suppose there is one to many relationship
I have two classes, the Group class has a many to many relationship with
I have two classes in an owned one-to-many relationship. The parent is Map, and
In nhibernate, I have two classes that are associated with a many-to-one mapping: <class
Supposing I have two classes, Customer and Order, where one Customer can have one-or-many

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.