http://nhforge.org/doc/nh/en/index.html#collections-onetomany <http://nhforge.org/doc/nh/en/index.html#collections-onetomany>
*Very Important Note:* If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, *you must use a bidirectional association* with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter. http://nhforge.org/doc/nh/en/index.html#collections-bidirectional Diego On Fri, Mar 12, 2010 at 16:58, tqwhite <[email protected]> wrote: > Thanks. I have been wondering if that might not be the best way. I > htink I read something different someplace to get me on the wrong way. > > I have implemented the "dual one-to-many" strategy. But, I get an > error message saying that I, "Cannot insert the value NULL into > column". That column is the foreign key in the join table. Basically, > NH is not getting the id from the objects I am trying to connect to > the join table. > > I am loathe to ask anyone to read much code, but I am clearly not > getting some important thing, so please be patient. > > Here is what I have done: > > <class ... student > <!-- all kinds of stuff that works except for this problem, id is > RefId, a uuid.hex --> > > <bag name="schools"> > <key column="SchoolInfoRefId"/> > <one-to-many class="AssemblyName.Models.StudentSchoolEnrollment, > AssemblyName" /> > </bag> > > </class> > > <class ... school > <!-- all kinds of stuff that works except for this problem, id is > RefId, a uuid.hex --> > > <bag name="students"> > <key column="StudentPersonalRefId"/> > <one-to-many class="AssemblyName.Models.StudentSchoolEnrollment, > AssemblyName" /> > </bag> > </bag> > > </class> > > <class ... StudentSchools ... > <!-- id and one property that is not controversial, but I haven't > built a separate test, id is RefId, a uuid.hex --> > > <many-to-one class="AssemblyName.Models.Student, AssemblyName" > name="school" column="StudentPersonalRefId"/> > <many-to-one class="AssemblyName.Models.School, AssemblyName" > name="student" column="SchoolInfoRefId"/> > > </class> > > Classes: > In School: public virtual IList<StudentSchoolEnrollment> > students{get;set;} //initialized in the contructor > > In Student: public virtual IList<StudentSchoolEnrollment> schools > { get; set; } //initialized in the contructor > > In StudentSchool: > > public virtual School school {get; set;} > public virtual Student student { get; set; } > > > To test it, I wrap the following in a session: > > Student newOne = new Student(); > NameOfRecordType newName = new NameOfRecordType(); > newName.FirstName="TQ"; > newName.LastName="White"; > newName.FullName="TQ White II"; > > newOne.Name = newName; > > > School newSchool = new School(); > newSchool.LocalId = "School #1"; > > StudentSchoolEnrollment newEnrollment = new > StudentSchoolEnrollment(); > newEnrollment.SchoolYear = "2010 and beyond"; > > newOne.schools.Add(newEnrollment); > newSchool.students.Add(newEnrollment); > > session.Save(newSchool); > session.Save(newOne); > session.Save(newEnrollment); > > If I remove the enrollment code (not removing xml), the school and > student are persisted correctly. If this were my bad, old sql days, > I'd be trying to figure out how to get private keys for school and > student, but am pretty sure NH is supposed to do that for me. > > Thanks for your help. > > tqii > > On Mar 12, 9:22 am, Diego Mijelshon <[email protected]> wrote: > > You'll need to create an intermediate entity, and one-to-many > relationships > > to it from the School and Student classes. > > > > Diego > > > > > > > > On Fri, Mar 12, 2010 at 12:08, tqwhite <[email protected]> wrote: > > > Is that a good way to title this? Do we have a better name for that > > > object that has both foreign keys? > > > > > Anyway, I am using a set of externally designed data types. Among them > > > is the connection between two of my entities, Schools and Students. > > > Each student can attend many schools. Each school has many students. > > > In addition, that period of attendance has a start and end date, etc. > > > > > I have constructed a mapping and it almost works. Two problems I need > > > help with: > > > > > 1) I am getting an error, "Cannot insert the value NULL into the > > > column RefId." It specifies the join table. RefId is the <id> column > > > for this intermediate. I bet that the problem is that NH doesn't > > > expect there to be an id on the joining table. > > > > > 2) I can't set said start and end dates because I can't find a way to > > > address that intermediate object. > > > > > I considered the idea that I should initialize that object in the > > > AddStudent() (and AddSchool()) method but the needed id keys don't > > > exist yet. > > > > > I spent last night poring over my brand, new copy of NHibernate in > > > Action (I LOVE THAT BOOK!), but can't find any advice. > > > > > Help! Please. > > > > > thanks, > > > tqii > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "nhusers" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]<nhusers%[email protected]> > <nhusers%[email protected]<nhusers%[email protected]>> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/nhusers?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
