-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping bidirectional list - error in reference docu?
PostPosted: Wed Feb 04, 2009 10:15 am 
Newbie

Joined: Thu Jan 25, 2007 12:32 pm
Posts: 6
I am experiencing another behavior than described in the
Hibernate Annotations Reference Guide, so I am wondering if there is a mistake in the Reference Guide.

I try to map a Parent-Child association with a bidirectional list.
Since I am using a list, the parent has to be the owning side of the relationship (no "mappedBy" possible).
I want Hibernate to produce optimal SQL. I expect two SQL-inserts and no SQL-update when creating a new Parent with one new Child.

I am using Hibernate Annotations for the mapping.
The Hibernate Annotations Reference Guide (Chapter 2.4.6.2.3) suggests the following solution:

On the Parent:
Code:
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id", nullable=false)
@org.hibernate.annotations.IndexColumn(name = "parent_index")
List<Child> children = new ArrayList<Child>();

On the Child:
Code:
@ManyToOne
@JoinColumn(name = "parent_id", updatable = false, insertable = false, nullable=false)
private Parent parent; 

But in this case Hibernate produces three SQL statements when persisting a parent with one child:
Code:
Hibernate: insert into Parent (name, version, id) values (?, ?, ?)
Hibernate: insert into Child (name, price, version, parent_id, parent_index, id) values (?, ?, ?, ?, ?, ?)
Hibernate: update Child set parent_id=?, parent_index=? where id=?


This is not the behavior that the Hibernate Annotations Reference Guide describes!


If I repeat the @JoinColumn attributes 'updatable = false' and 'insertable = false' from the Child to the Parent like this:
Code:
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id", updatable = false, insertable = false, nullable=false)
@org.hibernate.annotations.IndexColumn(name = "parent_index")
List<Child> children = new ArrayList<Child>();

Then Hibernate produces the SQL I am expecting:
Code:
Hibernate: insert into Parent (name, version, id) values (?, ?, ?)
Hibernate: insert into Child (name, price, version, parent_id, parent_index, id) values (?, ?, ?, ?, ?, ?)


I am wondering: Is there a mistake in the The Hibernate Annotations Reference Guide (Chapter 2.4.6.2.3) ?
Has there been a change in a recent Hibernate release?
Or am I missing something?

See also this thread for a complete code example.


Top
 Profile  
 
 Post subject: Re: Mapping bidirectional list - error in reference docu?
PostPosted: Fri Feb 06, 2009 12:01 am 
Newbie

Joined: Sun Oct 05, 2008 2:45 pm
Posts: 8
Hi,
Since parent table does not keep parent_id, you should not use @JoinColumn(name = "parent_id") in parent class.

Try to map like that and try to insert again.

Parent:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
public List<Child> getChildren() {
return children;
}

Child
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
public Parent getParent() {
return parent;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 2:20 pm 
Newbie

Joined: Thu Jan 25, 2007 12:32 pm
Posts: 6
Ok, I was not reading the Annotations Reference Guide thoroughly enough.

In Chapter 2.2.5.3.2.1 it is stated clearly:

Quote:
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 obviously not optimized and will produce some additional UPDATE statements.


It probably would not hurt to repeat this information in Chapter 2.4.6.2.3.

Now the question remains: if I repeat the @JoinColumn attributes 'updatable = false' and 'insertable = false' on the Parent (see code in first post) the additional update statements seem not to get produced... is this a legitimate workaround? Or does this result in another problem?


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