19.5.3. Bags and lists are the most efficient inverse collections
There is a particular case, however, in which bags, and also lists, are much more performant than sets. For a collection with
inverse="true"
, the standard bidirectional one-to-many relationship idiom, for example, we can add elements to a bag or list without needing to initialize (fetch) the bag elements. This is because, unlike a set
, Collection.add()
or Collection.addAll()
must always return true for a bag or List
. This can make the following common code much faster:
Parent p = (Parent) sess.load(Parent.class, id); Child c = new Child(); c.setParent(p); p.getChildren().add(c); //no need to fetch the collection! sess.flush();