-->
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: Foreign Key won't be set on related object/table
PostPosted: Mon Sep 26, 2005 1:52 pm 
Newbie

Joined: Mon Sep 26, 2005 1:42 pm
Posts: 2
Hi,

I am a NHibernate newbiy but I have experiences with Hibernate.

I have the following problem. Just one relation between a person and an email address:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="Skillworks.Common.Examples.NHibernate.Person, Skillworks.Common.Examples"
        table="PERSON">
        <id name="Id" column="ID" unsaved-value="0">
            <generator class="identity" />
        </id>
        <property name="Name" column="NAME" not-null="true" unique="false" />
        <!--<bag name="Emails" inverse="true" lazy="false" order-by="ADDRESS" cascade="all">
            <key column="PERSON_ID" />
            <one-to-many class="Skillworks.Common.Examples.NHibernate.Email, Skillworks.Common.Examples" />
        </bag>-->
       
        <!--<bag name="Emails" inverse="true" cascade="all" table="EMAIL">
            <key column="PERSON_ID"/>
            <one-to-many class="Skillworks.Common.Examples.NHibernate.Email, Skillworks.Common.Examples"/>
        </bag>-->
       
      <set name="Emails" cascade="all" lazy="true">
         <key column="PERSON_ID"/>
         <one-to-many class="Skillworks.Common.Examples.NHibernate.Email, Skillworks.Common.Examples"/>
      </set>
       
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="Skillworks.Common.Examples.NHibernate.Email, Skillworks.Common.Examples" table="EMAIL">
        <id name="Id" column="ID" unsaved-value="0">
            <generator class="identity" />
        </id>
        <property name="Address" column="ADDRESS" not-null="true" unique="false" />
      <many-to-one name="Person" column="PERSON_ID" class="Skillworks.Common.Examples.NHibernate.Person, Skillworks.Common.Examples" cascade="all" />
    </class>
</hibernate-mapping>


The problem is that when I try to save a person with an email I got an SQL error, that the column PERSON_ID can't be null. If I define the colum as "nullable" hibernate is making an entry into the person table and one in the email table but it won't set the foreign key. Now I don't know what I am doing wrong?!

Thanks for every hint.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 4:39 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
I think the cascade should only be on one side of the relation. Try setting cascade="none" on the Email.Person many-to-one element. You might also try putting inverse="true" on the Person.Emails set.


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.