-->
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: mappedBy vs JoinColumn and an IndexColumn
PostPosted: Wed Apr 21, 2010 5:17 am 
Newbie

Joined: Wed Apr 21, 2010 4:57 am
Posts: 4
I have a OneToMany association between two entities: Newsletter and Section. The order of the Sections within the Newsletter is important and needs to be persisted and I'm using the IndexColumn annotation.
Newsletter and Section code snippets are as follows.

Newsletter:
@OneToMany(mappedBy="newsletter")
@IndexColumn(name="sequence")
private List<Section> sections = new ArrayList<Section>();

Section:
@ManyToOne
@JoinColumn(name="newsletter_id", updatable=false, insertable=false)
private Newsletter newsletter;

However, this didn't persist the order of the Sections, the sequence column in the Sections table was null or zero. After some experimentation I changed Newsletter annotations to use JoinColumn, not the mappedBy attribute on the OneToMany annotation.

Newsletter using JoinColumn:
@OneToMany
@JoinColumn(name = "newsletter_id")
@IndexColumn(name="sequence")
private List<Section> sections = new ArrayList<Section>();

This version works, it sets the order of Sections in the sequence column in Sections table.

Can someone explain why this changed worked? What is the difference between mappedBy and JoinColumn? I'm happy it works, but the reason behind it is important and I don't understand why the IndexColumn fails to work when using mappedBy.

Many thanks.


Top
 Profile  
 
 Post subject: Re: mappedBy vs JoinColumn and an IndexColumn
PostPosted: Wed Apr 21, 2010 5:48 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
With using the mappedBy attribute you declare the Many side as owner of the relation.
In this case you will have no join-table (aka crosstable) on the schema and the Section table
just holds a reference to Newsletter:
field Section.newsletter_id
Sequence column in the Sections table is defined but is not filled by Hibernate.
I believe this is a limitation of Hibernate (probably the setting of the IndexColumn is only provided for the relation owner)

If you declare indeed the OneToMany relation as unidirectional (= without mappedBy attribute)
then the sequence column gets mapped on the join-table (crosstable) where it gets correctly filled and read
for maintaining the correct order.


Top
 Profile  
 
 Post subject: Re: mappedBy vs JoinColumn and an IndexColumn
PostPosted: Wed Apr 21, 2010 6:11 am 
Newbie

Joined: Wed Apr 21, 2010 4:57 am
Posts: 4
When I think of a join-table I think of a many-to-many association using a JoinTable annotation, not JoinColumn. Using JoinColumn makes the join-table the many side of association, not a extra table as in many-to-many, correct?

As this appears to be a limitation of hibernate, are there any reasons I should be aware of by NOT using mappedBy and using JoinColumn?

Many thanks.


Top
 Profile  
 
 Post subject: Re: mappedBy vs JoinColumn and an IndexColumn
PostPosted: Wed Apr 21, 2010 6:29 am 
Newbie

Joined: Wed Apr 21, 2010 4:57 am
Posts: 4
I have found the following in the documentation which basically answers my questions.

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping
To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is not optimized and will produce some additional UPDATE statements.


Top
 Profile  
 
 Post subject: Re: mappedBy vs JoinColumn and an IndexColumn
PostPosted: Wed Apr 21, 2010 7:46 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Using JoinColumn makes the join-table the many side of association


I don't understand the meaning of this your sentence.
Anyway: not only many to many relations require an extra table (=join table),
also unidirectional OneToMany relations require an extra table (=join table).


Top
 Profile  
 
 Post subject: Re: mappedBy vs JoinColumn and an IndexColumn
PostPosted: Wed Apr 21, 2010 8:45 am 
Newbie

Joined: Wed Apr 21, 2010 4:57 am
Posts: 4
Quote:
Quote:
Quote:
Using JoinColumn makes the join-table the many side of association


I don't understand the meaning of this your sentence.


Sorry that didn't make much sense. Let me try again.

Using JoinColumn puts an association into the many side of the one-to-many and it is that column that is used to join the two entities, not an extra table.

Many thanks.


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.