-->
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.  [ 9 posts ] 
Author Message
 Post subject: Saving the child with out fetching the parent object
PostPosted: Fri Apr 18, 2008 7:20 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Hi,

w.r.t to http://forums.hibernate.org/viewtopic.p ... 033737c282 and

http://www.hibernate.org/hib_docs/refer ... child.html

Is it possible to addd a new child record to child table, with out getting parent, assume that I know parents primary key.

Pl. suggest on how to do this with out using native sql. I would prefer to do it OOAD as much as possible, else hql solution.

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject: Re: Saving the child with out fetching the parent object
PostPosted: Fri Apr 18, 2008 8:28 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
nagkumar wrote:
Hi,

w.r.t to http://forums.hibernate.org/viewtopic.p ... 033737c282 and

http://www.hibernate.org/hib_docs/refer ... child.html

Is it possible to addd a new child record to child table, with out getting parent, assume that I know parents primary key.

Pl. suggest on how to do this with out using native sql. I would prefer to do it OOAD as much as possible, else hql solution.

Regards,
Nagendra


Hi,

yes, you can do that!

just create a new child object, load the parent object using it's identifier, set this last object as a parent to your child object and then save the child object in your current session.

cheers

_________________
savakos


Top
 Profile  
 
 Post subject: Re: Saving the child with out fetching the parent object
PostPosted: Fri Apr 18, 2008 8:37 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
>just create a new child object, load the parent object using it's identifier, set this last object as a parent to your child object

In this model, I am loading the full parent object.. I don't want this to happen as it a over head..specially assume that this parent has relation 10 other childeren etc..

if there is way to say hibernate that this child belongs to a partent whose id/pk is xyz then ask the child to be saved.. It should save the child, without loading the parent. I think is the most efficient approach and hence trying to get few clues

Regards,

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 10:11 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
well, to my knowledge I don't believe you can avoid this extra select statement when saving a child. That's because hibernate has to maintain the integrity of the association since one has been defined.

If, for instance, you set a non-persistent parent to a child (say a transient parent with only its id set) then hibernate has to verify that this parent exists indeed. If this check didn't occur then you would possibly be able to save a child referencing a fantom parent!! and this of course would seriously compromise the integrity of the association.

I don't know if anyone else knows a work-around for this but I would be surprised if one existed.


cheers

_________________
savakos


Top
 Profile  
 
 Post subject: nope.. need experts to suggest..
PostPosted: Fri Apr 18, 2008 4:22 pm 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Hi,

I think it should be possible with out loading, as this could mean very serious perf issue in practical applications. Most of them may have many children and each children to parent is many to one then loading all of them would be serious concern too..

If it does not exist, now I would expect hibernate team to consider this in future release and boost the performance by design..

Any other expert, who could back mosa's comments pl..

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 19, 2008 1:56 pm 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
well,

to my view this is not a flaw in hibernate's design because hibernate does its job effectively, which is maintaining the referential integrity for the defined association. If you try, for example, to insert a row with a phantom parent id at a child table with a one-to-many association defined, using plain DML in an SQL editor then you'll get an error. The underlying db manager of your database will complain that you are trying to insert a row referencing a non-existent parent. And that's the way it should be! So, hibernate simply does (and enforces) what a db manager would do.

Now,what could be modified for increasing performance is, I think, the way hibernates verifies a parent. So, instead of executing say "select parent.* from parent where parent.id=id", it could be like "select parent.id from parent where id=id" and an imaginary parentExists() method could return true if that scalar query returned a row. But, that's just an opinion from a user's point of view and only hibernate's designers know if this is a realistic suggestion....


About your problem I don't see any solutions unless of course you want to sacrifice the association....if this holds, you could drop the association completely and use the two tables as separate entities. Then, when you need to join the tables you can use theta-style joins.

Be aware though, that now YOU are responsible for the association and that object retrieval could be significantly complicated...

hope that helps

_________________
savakos


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 20, 2008 12:12 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
savkos,

I do see the value of maintaing the referencial integrity. However in my suggested approach of independent storing of child row by providing parents reference id only, the same thing should be possible.

Assuming the case of child parent ref pointing to invalid parent id, then database would any way will not allow it as this a forign key voilation.

Regards

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 20, 2008 10:14 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
I see your point,

but then again you would be assigning the management and maintainance of the db to the underlying db manager bypassing the hibernate layer. I'm not sure if this violates the design goals of hibernate. This is something that only a designer could answer I guess...

cheers

_________________
savakos


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 20, 2008 7:19 pm 
Newbie

Joined: Sat Mar 15, 2008 3:54 am
Posts: 6
Hi, from what I know it's unavoidable.
But, since you already have the key for parent, why not keep the whole parent as an detached object somewhere in your application(e.g. in http session). If you are going to use ORM, you gotta think ORM way. E.g. always refer to object rather than key/value etc. What I mean is, you should question yourself why you only have parent key not a complete parent object.
If you really only have parent key, well, you may want to use secondary cache to cache all parent. I haven't used that yet but I think it will keep all object id in cache so it MAY(not sure) avoid that unnecessary "select from parent".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.