I would just like to check with anyone out there that if they have a parent child relationship and if i want to add a child to a parent and the parent side of the r/s specifies inverse="true" cascade="all" and lazy="true", relationship is bidirectional and the code is something like below
Parent parent=(Parent)sess.load(Parent.class, new Long(parentId));
Child = new Child()
child.setParent(parent);
parent.addToChildren(child);
t.commit();
okay, when i set show_sql to true, i see hibernate doing a select on the parent which is correct, becos of the "sess.load(Parent.class, new Long(parentId));" and then it does an inset becos of the new child.
now in a normal jdbc way of doing things, i would only need 1 sql statement to insert the child row into the table cos the child table would have the parent foreign key. i wouldnt need to load the parent row cos i already have the parent id
but in the case of hibernate, since we are dealing with objects, i know it makes sense to load the parent and the set the parent in the child object and vice versa cos it is an object oriented way of doing things but is there a way to save the child in hibernate with just one sql statement instead? its more of an optimization kind of thing that i am asking here. so if anyone can provide a sensible answer, please do so not only for me but for others as well who might have the same question. Thanks you.
|