Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

I'm having the following issue when trying to update my entity:

"A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance".

I have a parent entity and it has a Set<...> of some children entities. When I try to update it, I get all the references to be set to this collections and set it.

The following code represents my mapping:

@OneToMany(mappedBy = "parentEntity", fetch = FetchType.EAGER)

@Cascade({ CascadeType.ALL, CascadeType.DELETE_ORPHAN })

public Set<ChildEntity> getChildren() {

    return this.children;

}

I've tried to clean the Set<..> only, according to this: How to "possible" solve the problem but it didn't work.

If you have any ideas, please let me know.

Thanks!

3 Answers

0 votes
by (46k points)
Actually, my problem was about equals and hashcode of my entities. A legacy code can bring a lot of problems, never forget to check it out. All I've done was just keep delete-orphan strategy and correct equals and hashcode.
0 votes
by (37.3k points)

The problem sometimes is overwriting the collection rather than adding to it. 

Doing like this can cause problems

post.dateActiveScheduleItems = list;

Instead, you can try the below-mentioned code:

post.dateActiveScheduleItems.addAll(list);

You should always work with only one instance of a collection. You should clear it or add to it, but never overwrite it.

Also,

Remove orphanRemoval=true from here.

@OneToMany(mappedBy ="schedule",cascade=CascadeType.ALL,orphanRemoval=true)

0 votes
by (1.2k points)

This problem occurs when the hibernate framework is not able to understand the relationship between a child and its parent entities. This majorly happens when we are trying to change the child collection. There are some things that can taken in consideration to remove or eliminate this issues:

  • Firstly, we need to make sure that there exists a reference from child to parent entity 

  • Moreover, we also need to make sure that the parent entity is also updated which is associated to the child. 

  • Additionally try not to update anything directly in the children entity, as it may lead to inconsistency. Rather than this, use the methods defined in the parent entity

Related questions

0 votes
1 answer
0 votes
2 answers
asked Oct 15, 2019 in Java by Shubham (3.9k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...