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
  • 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-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

Ask A Question

Stats

  • Questions 255k
  • Answers 255k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not tested, but... If you pass the help url (like… May 13, 2026 at 10:15 am
  • Editorial Team
    Editorial Team added an answer Is there some reason you can't just pass values for… May 13, 2026 at 10:15 am
  • Editorial Team
    Editorial Team added an answer Checkout the Akismet .NET 2.0 Api on CodePlex. Here's an… May 13, 2026 at 10:15 am

Related Questions

I have two classes A and B. Many B's can have association with a
I have a hibernate model with two classes, let's say A and B. B
This is not exactly a question, it's rather a report on how I solved
I can't seem to find an answer on this and just want to make
I actually have two questions regarding the same problem but I think it is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.