-->
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.  [ 6 posts ] 
Author Message
 Post subject: Bidirectional mapping difference between objects versus id
PostPosted: Tue Dec 22, 2015 3:54 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Hi,

I have a bidirectional mapping OneToMany between Forest and Tree with two tables Forest and Tree respectively. I see two options in mapping this association.

One approach is using pure objects:
Forest class having List<Tree> objects and Tree class having Forest object.

Second approach is:
Forest class having List<Tree> objects and Tree class having long field that stores forest_id, instead of having a reference to Forest object.

I tried with both the options and able to save and retrieve the objects and associations properly. The only advantage I see with first approach is I can navigate to Forest from Tree reference.

I am unable to understand what is the advantage of one approach over the time? I have always being following the first approach though in all my projects so far, but will I encounter any issues with second approach at any point? May be with second level cache in the picture.


Top
 Profile  
 
 Post subject: Re: Bidirectional mapping difference between objects versus id
PostPosted: Tue Dec 22, 2015 5:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The first approach is better because the child-side can control the association and it therefore performs better when removing/reshuffling child entities.
The second one works but it's practically a unidirectional bag, which most likely uses a link table instead of the foreign_key in the child table.


Top
 Profile  
 
 Post subject: Re: Bidirectional mapping difference between objects versus id
PostPosted: Tue Dec 22, 2015 6:46 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Not sure I got your point,
Quote:
child-side can control the association and it therefore performs better when removing/reshuffling child entities.
could you please elaborate on how would child-side controlling has better performance?

Also regarding,
Quote:
which most likely uses a link table instead of the foreign_key in the child table
. I am not using any link table and instead forest id is a foreign key in the child table (Tree).


Top
 Profile  
 
 Post subject: Re: Bidirectional mapping difference between objects versus id
PostPosted: Tue Dec 22, 2015 8:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The behavior of unidirectional bags is documented here: https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch20.html#performance-collections

By default, the unidirectional bags use a join table indeed. You can probably emulate the same behavior on an existing schema where the client table has a FK but you need to check how the child removal operations behave versus when using a bidirectional association.


Top
 Profile  
 
 Post subject: Re: Bidirectional mapping difference between objects versus id
PostPosted: Tue Dec 22, 2015 8:49 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Thanks Vlad. I think I got it and want to make sure that we are on the same page.

With Unidirectional map using the join table, the issue with child deletes is that it removes all the existing associations from the join table and then adds them again with the updated list.

With my option 2, with unidirectional map and no join table and child table emulating FK, I see two queries being fired as below to delete a child record (Tree) as below, an extra query:

Code:
update Tree set FOREST=null where FOREST=? and id=?
delete from Tree where id=? and version=?


And with option 1, with bidirectional mapping and ManyToOne owning the relationship, I see only one query being fired as below to delete a child record (Tree):

Code:
delete from Tree where id=? and version=?


Correct me if I got it wrong.


Top
 Profile  
 
 Post subject: Re: Bidirectional mapping difference between objects versus id
PostPosted: Tue Dec 22, 2015 10:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's what I was talking about. It's all about efficiency and the bidirectional association or the unidirectional @ManyToOne provides better DML statements.


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