Hi,
I'm trying to understand how persistence works and have been experimenting with a small app. Please read on and help me to understand if my findings are correct.
Note that given a class Foo that has a collection of Bar objects like so:
@Entity
@Table (name = "FOOS")
public class Foo
{
...
@OneToMany(mappedBy = "foo")
private Collection<Bar> BARS = new ArrayList<Bar>();
...
//getters and setters
}
Let us say that we have loaded the collection with three Bar objects. Now, let us say that we want to remove one of the Bar objects from the collection. My experimental app seems to indicate that it is not enough to simply call the remove(Object o) method on the collection, but one has to also call the remove(Object o) method on the EntityManager as well in order to get that record out of the BARS table of the database. Correct?
Furthormore, what if I wanted to remove the entire instance of the Foo object (this would include any and all remaining Bar objects in the collection)? Do I have to clear the entire collection of Bars first and those instances from the BARS table before I can remove the instance of the Foo object from the FOOS table of the database? Is there a way to do this all in one go with one method call? Does cascading help in some way in this scenario?
Please advise,
Alan