-->
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.  [ 4 posts ] 
Author Message
 Post subject: One way association & not null insert problem
PostPosted: Tue May 15, 2007 1:27 am 
Newbie

Joined: Tue Mar 27, 2007 11:34 pm
Posts: 13
Hibernate version: NHibernate 1.2

Database Schema :
Course (ID, Code)
Student (ID, Name, CourseID)
PS: All the column is not null

Course Mapping:
Code:
<class name="school.Course, SchoolApp" table="Course" lazy="false">
   <id name="ID" column="ID" type="Int32" unsaved-value="0">
      <generator class="native"/>
   </id>
   <property name="Code" column="Code" type="int" length="10" not-null="true"/>
   <set name="Students" lazy="true" cascade="save-update" inverse="false">
      <key column="CourseID"/>
      <one-to-many class="school.Student, SchoolApp"/>
   </set>
</class>


Student Mapping:
Code:
<class name="school.Student, SchoolApp" table="Student" lazy="false">

   <id name="ID" column="ID" type="Int32" unsaved-value="0">

      <generator class="native"/>

   </id>

   <property name="Name" column="Name" type="string" length="30" not-null="true"/>


</class>


I enabled the show SQL properties, when I save the Student object, the CourseID will not inserted. Seem it is not null, so the error will occur.

In java hibernate, i can solve the problem by define the not-null property in the key.
<key column="CourseID" not-null="true"/>

But seems NHibernate not support the not-null in key. Did anyone know how to solve this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 3:45 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
If you have not null field like that, you need it in your mapping as well.

Code:
   <many-to-one name="Course" class="school.Course, SchoolApp" column="CourseID" not-null="true"/>


See documentation under: 6.11. Collection examples


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 5:53 am 
Newbie

Joined: Tue Mar 27, 2007 11:34 pm
Posts: 13
It mean that it must be bi-directional association?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 6:03 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
If you have a not null constraint, yes.


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