I'm getting so beaten up and confused by this. I have never had this
much trouble getting something to work.
I actually considered the not-null issue before. At one point, I
asserted not-null="false" in all the places but it made no difference.
I just did it again and found that it generates the same schema either
way. Probably a bug, but it's true that I also didn't check. Arrgggh.
So, I read the bidirectional association. As best I can tell, the only
difference is that I didn't add the attribute, inverse="true" to the
many side, aka, bag. I tried this at one point before as a pure random
experiment. As with tonight, it didn't solve the problem.
Still, I did it again:
<bag name="students" inverse="true">
<bag name="schools" inverse="true">
And I still get the null violation.
So, I know these things:
1) My mapping basically works. When I manually remove "not null" and
the foreign key constraints from my database, I get the database
results I want.
2) Inverse="true" added to the bag, doesn't do it.
3) NH can do this, so I must have something wrong that I am can't
find.
Here is what the manual says, interleaved with notes:
<class name="Eg.Parent, Eg"> <!-- For me, Parent is the student and
school entities, one student/school has many enrollments-->
<id name="Id" column="id"/>
....
<set name="Children" inverse="true" lazy="true"> <!-- I use a bag
since I don't want them ordered and I couldn't get the type right when
I tried using a set for fun. The docs, and common sense, say this is
ok. Right?-->
<key column="parent_id"/> <!-- I use SchoolInfoRefId (or
StudentInfoRefId both here and in the many-to-one-->
<one-to-many class="Eg.Child, Eg"/>
</set>
</class>
<class name="Eg.Child, Eg"> <!-- for me, this is the StudentSchool
join table. Many enrollments can relate to a single student/school. --
>
<id name="Id" column="id"/>
....
<many-to-one name="Parent" class="Eg.Parent, Eg"
column="parent_id"/>
</class>
I just cannot see anything wrong with my code. I've been at this for
the last four hours. I'm dying.
tqii
On Mar 12, 3:36 pm, Diego Mijelshon <[email protected]> wrote:
> 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%252bunsubscr...@googlegroup
> > s.com>>
> > > > .
> > > > 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.