-->
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: one to many foreign key not being inserted
PostPosted: Wed Mar 18, 2009 9:32 am 
Newbie

Joined: Wed Mar 18, 2009 7:16 am
Posts: 2
I'm trying to create a standard parent-child relationship, code:

Code:
public class ChangeRequest : DomainEntity
{
//...snip
    private IList<FurtherComment> m_FurtherComments = new List<FurtherComment>();
    public IList<FurtherComment> FurtherComments
        {
            get
            {
                return m_FurtherComments;
            }
        }

     public void AddFurtherComment(DateTime postedAt, string postedBy, string content)
     {
            m_FurtherComments.Add(new FurtherComment(postedAt, postedBy, content));
     }
}

public class FurtherComment : DomainEntity
{
//...snip
}


mapping in ChangeRequestItem.hbm.xml:

<bag generic="true" name="FurtherComments" cascade="all-delete-orphan" access="field.pascalcase-m-underscore">
<key column="ChangeRequestItemId" />
<one-to-many class="FB.CRS.Core.FurtherComment, FB.CRS.Core" />
</bag>

As can be seen the relationship is uni-directional from the ChangeRequestItem object, however I run into an issue when adding the FurtherComment object to the ChangeRequestItem whereby the foreign key value is not being inserted into the FurtherComment's table, so I get the following exception:

Cannot insert the value NULL into column 'ChangeRequestItemId', table 'CRS.dbo.CRSFurtherComment'; column does not allow nulls. INSERT fails.
The statement has been terminated.

After some digging I found the following on stackoverflow http://stackoverflow.com/questions/5558 ... ny-problem which appears to be the same issue, in the answers section he solves it by making the relationship bi-directional, is this really the only way? I don't really want to have the bi-directional relationhip as it makes no sense in the domain model.

Any help is appreciated.

Thanks,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 3:48 pm 
Newbie

Joined: Thu Mar 19, 2009 10:16 pm
Posts: 5
I have the same issue posted on another thread and have yet to get a response that solves the issue but I did locate a post that does explain a possible "fix"

Posted: Fri Mar 20, 2009 7:06 pm Post subject:

http://bchavez.bitarmory.com/archive/20 ... ibute.aspx


In the unidirectional case, the foreign key column in the child table doesn't map to a property in the child object; it is in the data model, but not the object model. Since it is unidirectional there is just a property in the parent object [that contains a collection of children]; not the child [the child object does not contain any information back to the parent]. In addition,
Quote:
the foreign key column [in the child table] has to be defined as nullable
because Hibernate will first insert the child row (with a NULL foreign key) and then update it [the inserted row] later [with the parent's primary key].


I found this post explaining what I wish to accomplish and was a bit put put off by the statement "In addition, the foreign key column[in the child table] has to be defined as nullable because Hibernate will first insert the child row". From a DBA perspective this is a huge no-no and in most companies would not be accepted.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 5:51 am 
Newbie

Joined: Wed Mar 18, 2009 7:16 am
Posts: 2
Thanks for the reply, that's a really good article and explains the issue well, in the end I went for the nullable foreign key as I have full control over the DB.

My understanding seems that there is a trade off were you either accept the nullable foreign key and keep it uni-directional or change the domain to be be bi-directional and make the child responsible for setting the relationship.


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.