-->
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.  [ 2 posts ] 
Author Message
 Post subject: Unidirectional Mapping - Null Problem
PostPosted: Thu May 27, 2004 8:00 pm 
Newbie

Joined: Thu Apr 15, 2004 5:53 pm
Posts: 13
If I create a parent object with a name, and a set of children, and a child object with a name, this is a unidirectional one-to-many relationship pretty similar to what's shown in the documentation.

Now, theoretically, since the Child is persistable alone, its parent key should be nullable. In practice, I can control this to prevent that from happening, by only allowing the external API to pass a Parent to be saved, with no call to save the Child directly.

So if I were creating a schema that works with this structure, I might set the parent key to not-null, even though a Hibernate schema generation would make it nullable by default. There is no way to inform hibernate of this fact in the mapping since the <key> tag inside <set> can't take a not-null attribute.

If I then save a Parent with a Child, it inserts the Parent, inserts the Child (with no Parent key) and updates the Child to include the parent key. If I've made the parent key not null, this immediately breaks and causes a failure.

Is there any way around this other than making it a bidirectional link? It's not the end of the world if that's what I have to do, and it does increase the safety of saving a child directly (by allowing me to specify that the parent key is not null).

I had been leaning towards unidirectional links when not otherwise necessary, just to get around the consequence of having one of them be 'ignored', but this may prevent me from going that route.

Thoughts?

- Geoffrey

_________________
--
Geoffrey Wiseman


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 8:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, a bidirectional class association reflects the not-null foreign key constraint. There is no way around it. What should Hibernate insert? A child can be referenced by many parents.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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