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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:25:26+00:00 2026-05-23T21:25:26+00:00

If a parent object starts off with a list of 3 children objects, removes

  • 0

If a parent object starts off with a list of 3 children objects, removes child 1 and calls session.update(parent), what kind of hibernate mapping would I need in order to clear the parent PK from the removed child table without deleting the removed child completely?

I also have a constraint which prevents any parent from being deleted if any child is assign to it and another constraint that prevents me from accidentally cascade-deleting any child entries.

I can successfully cascade update children by simply calling session.update(parent), but I’m having trouble doing the reverse.

public class Parent implements Serializable {
  private static final long serialVersionUID = 1L;
  private Long id;
  private List<Child> children;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @OneToMany(mappedBy = "parent")
  @org.hibernate.annotations.Cascade( 
        value = {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
  public List<Child> getChildren() {
    return children;
  }

  public void setChildren(List<Child> children){
    this.children = children;
    for(Child child : children) {
      child.setParent(this);
    }
  }

  //-----------------   Would I need something like this?  ---------------
  //----------------- Or does hibernate have a better way? ---------------
  public void setChildren(List<Child> children){

    for(Child child : this.children)
        if (!children.contains(child)) {
          child.setParent(null);
        }
    }
    this.children = children;
    for(Child child : children) {
      child.setParent(this);
    }
  }
  //----------------------------------------------------------------
}

public class Child implements Serializable {
  private static final long serialVersionUID = 1L;
  private Long id;
  private Parent parent;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @ManyToOne
  public Parent getParent() {
    return parent;
  }

  public void setParent(Parent parent) {
    this.parent = parent;
  }
}

This solution is similar, but I’m not deleting the Parent, instead I’m removing a Child from the List:

hibernate cascade – update child to null

  • 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-23T21:25:26+00:00Added an answer on May 23, 2026 at 9:25 pm

    If you don’t have a not-null constraint on the FK parent field in the child table, you can just set parent to null and save the child.

    I’d write the following method:

    Boolean removeChild(Child c) {
        Boolean success = getChildren().remove(c);
        if(success) c.setParent(null);
        return success;
     }
    

    I’d be careful about overwriting the setChildren method, as hibernate needs plain getters and setters.

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

Sidebar

Related Questions

I have a parent object called Page that has a List of objects called
Can I automatically validate complex child objects when validating a parent object and include
I´ve got this on the parent object @OneToMany(mappedBy=idUser, cascade = CascadeType.MERGE) public List<Directions> directions;
Hi i want to cast the Parent object to Child object in C# public
I have a object that dynamically creates a number of child objects. These child
I have a parent object Compound with a one-to-many relationship to an object Submission.
is there a way to traverse an object to get the parent object data?
Typical problem - I am trying to save new parent object with nested new
how can i reference a display object's coordinates according to it's parent object or
I would like to know how to convert parent object that was return by

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.