I have one to many mapping between School and Student entity’s.
public class School{
@oneToMany
private List<Student> students;
...
}
It makes sense that Hibernate by default fetches student lazily, but I’m not clear to me why it loads its own implementation of List (PersistantBag) instead of java ArrayList?
A List is an ordered collection. A PersistantBag on the other hand (according to the Hibernate API) is:
In other words a bag is just a random collection of the items in the list.
In order for Hibernate to treat your List as an ordered collection it would need an index column (see Indexed Collections). But since you don’t have such a column Hibernate instead uses “bag semantics”. In the end there isn’t too much difference to you, the developer, since you are likely not relying on the order but if you do need it you can add an index column to get these features.
Another nice feature that a bag has is that you can get the number of occurrences of an object in your collection, whereas in a List you would need to iterate over the whole list to find that out.
Additional Resources:
Why Hibernate does “delete all then re-insert” – its not so strange