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: Single direction association insert/update behavior
PostPosted: Sat Sep 22, 2007 10:30 am 
Newbie

Joined: Sun Nov 26, 2006 4:08 pm
Posts: 10
Location: Seattle, WA
I've noticed that if you have only a one-to-many association between a parent and its children that when you create a new child and save it, it will first insert the new row with null in the FK. It will then flush the parent, and update that row to include the parent's PK value in the FK.

This completely breaks any attempt to do FK constraints or NULL constraints on the FK column.

If I put the many-to-one on the child and set the parent's one-to-many to inverse=true, then this works as expected. I don't want to have to make all my collections bidirectional just for this reason, especially since adding new elements is an awkward two-step process (add to collection, set parent on child).

Is there something I'm missing here? I've tried all the cascade options on the parent, is there something else I need to do to make this work as I'd expect or is this by design?

Thanks,

Aaron


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 10:50 am 
Newbie

Joined: Sun Nov 26, 2006 4:08 pm
Posts: 10
Location: Seattle, WA
Apparently this has been answered before, but I still can't find it in search. Basically, NHibernate updates collections separately from contained objects. This means that, like I feared, this is by design and the only way around it is to make the associations bidirectional. This may have been improved in H3.2, but it may never be ported... so ah well, we're stuck w/ this.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 12:03 pm 
Newbie

Joined: Wed Jun 07, 2006 3:15 am
Posts: 14
I have the same issue. Hibernate FAQ states:

Quote:
Q: I'm using a one-to-many association with a NOT NULL constraint on the key column and Hibernate causes constraint violations.

A:Use a bidirectional association, mapping the many-valued end with inverse="true".
Or, for a unidirectional association, use <key not-null="true" ... /> in the collection mapping.


Unfortunately <key not-null="true" ... /> is not supported in NHibernate as it seems. I don't know if there is a workaround.


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.