-->
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
PostPosted: Wed Jan 09, 2008 12:10 pm 
Newbie

Joined: Wed Jan 09, 2008 11:28 am
Posts: 1
The FAQ says:

I'm using a one-to-many association with a NOT NULL constraint on the key column and Hibernate causes constraint violations.
...
Or, for a unidirectional association, use <key not-null="true" ... /> in the collection mapping.

However, it seems not-null isn't an attribute for key. My program works if I remove the constraint, but I'd rather not do that and keep my dba happy if I can. I'm attempting to put it on the key column under the bag:


<bag name="SomeValues" table="hibernatechilld" cascade="all">
<key not-null="true" column="parent_id" />
<one-to-many class="HibernateTest.someValue, HibernateTest"/>
</bag>

I get an error stating that's not valid. What's the real answer for unidirectional, or is bi-directional a requirement?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 1:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
A unidirectional one-to-many indeed requires the foreign key column to be nullable.

There are two options to work around that: First, make the association bi-directional. Secondly, if the elements in the collection does not need to be full-fledge entities (i.e. they live and die with the parent object), you may use <composite-element> instead of <one-to-many>.

The first option is arguably easier to implement; the second option requires some careful considerations on the collection types (e.g. bags vs. sets) and equality semantics.

_________________
Karl Chu


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.