-->
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.  [ 5 posts ] 
Author Message
 Post subject: unnecessary update on unidirectional one-to-many association
PostPosted: Wed Dec 12, 2007 6:44 am 
Newbie

Joined: Tue Mar 21, 2006 7:18 pm
Posts: 13
Hi,

I have read the Parent-Child Principles of Hibernate (http://www.hibernate.org/209.html).

There bi-directional one-to-many associations are discussed mainly.

I have an uni-directional one-to-many association. (There is no reference from the child to the parent in java. And there ist no many-to-one mapping in the mapping hbm of the child) And I have a problem that is described there as well:

Note that uni-directional one-to-many will fail with a forgien key constraint
violation unless the forgein key column in the database is null-able. This is becuase Hibernate first inserts the child row (with a NULL foreign key) and then updates it later.

What is the solution for such an association? To declare the columns as null-able?
This is a bad solution?

You can alternativly declare not just the columns as not-null-able, but also declare the key in the one-to-many mapping als not-null-able.

Hibernate then produces two right insert sqls:
insert the parent
insert the child (with the correct pk of the parent as fk)

and one unnucessary update sql:
update child set pk of parent as fk

The information of the update is allready inserted in the insert statement of the child.

Is it possible to avoid the update statement? Because it does nothing.

Or am I missing something?
Is the update sql needed?

It seems that setting the key as not-null-able makes the update statement obsolete?

Thanks.

jakob


Top
 Profile  
 
 Post subject: Re: unnecessary update on unidirectional one-to-many association
PostPosted: Mon Feb 20, 2012 10:53 am 
Newbie

Joined: Mon Feb 20, 2012 10:51 am
Posts: 2
4 years later, and I am looking for an answer to this same question.


Top
 Profile  
 
 Post subject: Re: unnecessary update on unidirectional one-to-many association
PostPosted: Mon Feb 27, 2012 2:27 am 
Newbie

Joined: Tue Aug 02, 2011 2:32 am
Posts: 1
Even I am trying to find the solution to this.. does the community acknowledge this scenario


Top
 Profile  
 
 Post subject: Re: unnecessary update on unidirectional one-to-many association
PostPosted: Wed Jul 18, 2012 2:45 pm 
Newbie

Joined: Tue May 03, 2011 10:57 am
Posts: 4
Location: Bucharest, Romania
I am running into a very similar scenario (not sure if it's exactly the same); still unresolved as well: http://stackoverflow.com/questions/1153 ... 5#11542325


Top
 Profile  
 
 Post subject: Re: unnecessary update on unidirectional one-to-many association
PostPosted: Thu Apr 21, 2016 9:22 am 
Just set updatable=false, most of the time it make sense.
@OneToMany @JoinColumn(updatable=false, nullable=false)


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