Looking at the chapter on Parent/Child relationships, the recommended way of adding a child is with the following code:
Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(c);
My question is, wouldn't it be more efficient to do a Hibernate.isInitialized() on the collection first and only add to it if it is already initialized? Why force an additional SQL query to get the collection if it may not even be needed (e.g. when the add() is called, this would force a SQL query to lazily load the collection before the new element can be added)?
So my code would become:
Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
if (!Hibernate.isInitialized(p.getChildren()) {
p.getChildren().add(c);
}
session.save(c);
If the program needs to collection later on, the lazy init SQL could be performed then, and the new element would be included in that result set.
Are there any possible problems doing it this way, or is not recommeded for any reason?
Thanks,
Daniel[/b]