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 8627709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:21:04+00:00 2026-06-12T08:21:04+00:00

I have a tree with parent-child relation implemented with @OneToMany and @ManyToOne annotations. Every

  • 0

I have a tree with parent-child relation implemented with @OneToMany and @ManyToOne annotations.

Every child has @ManyToOne parent field, every parent has @OneToMany.

Both children and parents are heterogeneous, entities of any type may belong to parents of any parent type.

In this situation, Hibernate creates and manages the relation with an additional parent_child table, like it does for @ManyToMany relations.

Everything went well (I was able to create and delete any object), until I tried to implement changing the object parent, and got constraint violation.

The operation was implemented as follows:

oldParent.children.remove(child);
child.parent = newParent;
newParent.children.add(child);

As expected, Hibernate generates 3 change request: one to update the child.parent value, and two to update children collections for old and new parents.

The problem is in the wrong order of collection updates: it tries to add a new relation before removing the old one, which causes a constraint violation, since two parents are not allowed.

How can I force the right order of updates? Or how can I avoid using an additional table in this situation?

  • 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-06-12T08:21:05+00:00Added an answer on June 12, 2026 at 8:21 am

    After your first remove you should call flush. This will remove the initial parent relationship. Now if you reload the parent and child you should be able to associate them with each other.

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

Sidebar

Related Questions

I have a tree structure where each Node has a parent and a Set<Node>
I have a category/file tree structure. Both categories and files can have parents, so
I have a parent/child table (simple tree) table structure (ID, ParentID), where I want
I have a tree structure like below. when i travese through parent to child
I have a tree structure that has a node with a parent ID (unlimited
i have tree list( one parent and 4 children), if i change the children
I have a parent/child relation table, many to many parentid int not null childid
I have an object-oriented parent-child tree in PHP that I want to clone. The
I have a tree data structure composed by parent nodes and child nodes, something
I have tree like following example, where every leaf is object. [1] |--[2] |

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.