-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate produces more sql statements?
PostPosted: Sat Oct 23, 2004 9:09 pm 
Newbie

Joined: Thu Oct 21, 2004 8:13 am
Posts: 8
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 24, 2004 9:49 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
This is true of hibernate generally speaking. If you were to enable proxy for your parent class, used session.get instead of session.load, and your collection is mapped to a bag/list, you might be able to get around this problem... if you see it as such.
Another possibility, if you feel this is detrimental to performance, you might be able to get away with the following:

Parent p = new Parent();
p.setId(incomingId)
Child c = new Child();
c.setParent(p)
c.save();

Not a very oo way of doing it, but it will work.
I prefer doing it the other way, as this allows me to unit test my objects (like the add child method) w/out the need of a database. If there is bus logic attached to the add method, testing can be achieved without putting data in the db.

--James


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.