Thanks for the response. Below are the mapping files.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateTest"
namespace="NHibernateTest.Domain">
<class name="UniversityClass"
table="universityclass">
<id name="Id" column="classid" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="classname"
type="String(50)"/>
<property name="Number" column="classnumber"
type="String(10)"/>
<property name="Syllabus" column="syllabus"
type="String(50)"/>
<property name="StartDate" column="startdate"
type="datetime"/>
<many-to-one name="Department"
class="Department"
column="deptid"/>
<many-to-one name="Professor"
class="Professor" cascade="all" column="personid"
not-null="true"
lazy="false"/>
<bag name="Students" table="studentclass">
<key column="classid"/>
<many-to-many class="Person"
column="personid"/>
</bag>
</class>
</hibernate-mapping>
and
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateTest"
namespace="NHibernateTest.Domain">
<class name="Person" table="people">
<id name="Id" column="personid" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<discriminator column="persontype" type="String"/>
<property name="FirstName" column="firstname"
type="String(50)"/>
<property name="LastName" column="lastname"
type="String(50)"/>
<subclass name="Professor"
discriminator-value="professor">
<property name="Identifier" column="identifier"
type="String"/>
<bag name="Classes" table="UniversityClass" inverse="true"
generic="true">
<key column="personid"/>
<one-to-many class="UniversityClass"/>
</bag>
</subclass>
<subclass name="Student"
discriminator-value="student">
<property name="SSN" column="identifier"
type="String"/>
<bag name="Classes" table="studentclass">
<key column="personid"/>
<many-to-many class="Person"
column="classid"/>
</bag>
</subclass>
</class>
</hibernate-mapping>
This is based on an example I found on the internet which covers most
of the basic database relationships. I am trying to work out what I
can and can't do with Nhibernate before I attempt to use it in a
project. The point where I was at was trying to create a University
Class object then create a Professor object and add it to the
University Class object. Once this was done I wanted to save the
University Class object to the database and retrieve it. It works
fine if the Professor object is not lazy loaded but I can't seem to
get data back if it is.
My code is:
var uniClass = new UniversityClass { Name = "Chemistry A
Level", StartDate = Convert.ToDateTime("10/10/2009")};
Professor prof = new Professor();
prof.Id = -1;
prof.FirstName = "Mark";
prof.LastName = "Ufland";
prof.Identifier = "professor";
uniClass.Professor = prof;
IUniversityClassRepository repository = new
UniversityClassRepository();
repository.Add(uniClass);
// use session to try to load the product
using (ISession session = _sessionFactory.OpenSession())
{
var fromDb = session.Get<UniversityClass>
(uniClass.Id);
// Test that the product was successfully
inserted
Assert.IsNotNull(fromDb);
Assert.AreNotSame(uniClass, fromDb);
Assert.AreEqual(uniClass.Name, fromDb.Name);
NHibernateUtil.Initialize(fromDb.Professor);
string name = fromDb.Professor.LastName; //****This
bit does not work if lazy loaded****
Assert.IsFalse(NHibernateUtil.IsInitialized
(fromDb.Professor));
}
Cheers
On Nov 13, 9:09 am, Oskar Berggren <[email protected]> wrote:
> 2009/11/11 Fluff <[email protected]>:
>
>
>
> > Hi,
>
> > I am pretty new to NHibernate and have been playing around with it to
> > see how it works. It looks pretty good and I have got most things
> > working but I have come accross a lazy loading thing that I need
> > clarifying.
>
> > I have a one to many relationship and I am creating an instance of the
> > "many" class and an instance of the "one" class which is a child of
> > the "many" class.
>
> Hmm.... normally it would be the child that is the "many"-side. One
> parent has many children.
>
> > Now, as far as I can tell I have wired up the
> > mapping files properly and when I save the instance of the "many"
>
> Maybe you have, maybe you don't... Without posting your mappings we
> can't verify. :)
>
> > class to the database the cascade saves the instance of the "one"
> > class as well. That's great. However when I pull everything back from
> > the database the instance of the "one" class has no data in it. So I
> > thought OK it's doing it's lazy loading thing and if I access the data
> > in the class Nhibernate will load it. Apparantly not, and I can't get
> > it to load the data unless I set lazy to false on the "one" class
> > mapping file. Then it all works. As far as I can tell the session is
> > still open so the lazy loading thing should work but as I said I am
> > new to this.
>
> > Any help would be appreciated.
>
> /Oskar
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---