In my application I have two entity Person and Activity
@Entity
public class Person
{
.
.
.
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,orphanRemoval=true)
Set<Activity> a;
// getters setters constructors
}
... and...
@Entity
public class Activity
{
.
.
.
// getters setters constructors
}
The result is a Person object with Set<Activity>
with size=2
.
Can someone help me please?
When you remove the Activity object from the database you have to update the set in Person and remove that Activity object also from the set. Otherwise you will have a broken reference to a non existing Activity object in the set.
On the other hand, since you enabled orphanRemoval, you can just remove the Activity from the set in Person and it should be removed automatically also from the database.
Another option is to define the set as mappedBy and then the set will be updated automatically when an Activity is removed from the database, when the Person object is loaded again from the database or refreshed.