-->
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: Lazy loading & one to many relationship
PostPosted: Mon Oct 17, 2005 4:25 am 
Newbie

Joined: Mon Oct 17, 2005 4:16 am
Posts: 4
Hi,

Desperately need help as I've reviewed all examples, tried numerous things and still unable to solve my seemingly very simple problem.

All I am doing is persisting 2 objects and then trying to load the "parent" object and am expecting the child to be lazy loaded, but do not get anything... The records in the db tables have correct values.. so not sure whats wrong here.

hbm.xml files and c# below:

------------
person.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nHibernateTest.Person, nHibernateTest" table="person">
<id name="Id" column="person_id" type="Int32">
<generator class="assigned" />
</id>
<property name="Name" column="name" type="String" length="50"/>
<property name="Age" column="age" type="Int32"/>
<property name="Salary" column="salary" type="Decimal"/>
<bag name="Addresses" lazy="false" inverse="true" cascade="all">
<key column="person_id"/>
<one-to-many class="nHibernateTest.Address, nHibernateTest"/>
</bag>
</class>
</hibernate-mapping>

------------
Address.hmb.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="nHibernateTest.Address, nHibernateTest" table="address">
<id name="Id" column="address_id" type="Int32">
<generator class="increment" />
</id>
<property name="Street" column="street" type="String" length="50"/>
<property name="Suburb" column="suburb" type="String" length="50"/>
<many-to-one name="thisPerson" column="person_id" not-null="true"/>
</class>
</hibernate-mapping>

------------

Person.cs

public class Person
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}

private string name;
public string Name
{
get { return name; }
set { name = value; }
}

private int age;
public int Age
{
get { return age; }
set { age = value; }
}

private decimal salary;
public decimal Salary
{
get { return salary; }
set { salary = value; }
}

private IList addresses = new ArrayList();
public IList Addresses
{
get { return addresses; }
set { value = value; }
}

public Person() {}

}



------------

Address.cs

public class Address
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}

private string street;
public string Street
{
get { return street; }
set { street = value; }
}

private string suburb;
public string Suburb
{
get { return suburb; }
set { suburb = value; }
}

private Person person;
public Person thisPerson
{
get { return person; }
set { person = value; }
}

public Address() {}
}


------------

webform1.aspx.cs

protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button1;
private ISessionFactory _sessions;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Configure();
//ExportTables();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
ValidateQuickStart();
}

public void ValidateQuickStart()
{
ISession session = _sessions.OpenSession();
ITransaction transaction = session.BeginTransaction();

Person newP = new Person();
newP.Id = 1;
newP.Name = "Joseph Cool";
newP.Age = 35;
newP.Salary = 35000M;

session.Save(newP);
session.Flush();

Address newA = new Address();
newA.thisPerson = newP;
newA.Street = "st kilda";
newA.Suburb = "elsternwick";
newP.Addresses.Add(newA);

// Tell NHibernate that this object should be saved

session.Save(newA);

// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();

// open another session to retrieve the just inserted user

}

public void draw()
{
ISession session = _sessions.OpenSession();

IList recentPersons = session.CreateCriteria(typeof(Person))
.Add(NHibernate.Expression.Expression.Gt("Salary", 45000M))
.List();

foreach(Person p in recentPersons)
{
Response.Write("<br><BR>" + p.Name);
if (p.Addresses.Count > 0)
{
foreach (Address a in p.Addresses)
{
Response.Write("<br>" + a.Street + " " + a.Suburb);
}
}
Response.Write("<hr>");
}
Response.Write("-");
session.Close();
}

private void Button2_Click(object sender, System.EventArgs e)
{
draw();
}

public void Configure()
{
Configuration cfg = new Configuration();
cfg.AddAssembly("nHibernateTest");
cfg.AddXmlFile(@"C:\Dev\Projects\nHibernateTestSolution\nHibernateTest\Person.hmb.xml");
cfg.AddXmlFile(@"C:\Dev\Projects\nHibernateTestSolution\nHibernateTest\Address.hmb.xml");
_sessions = cfg.BuildSessionFactory();
}
}

------------

What am I doing wrong?


Top
 Profile  
 
 Post subject: revision to post
PostPosted: Mon Oct 17, 2005 4:37 am 
Newbie

Joined: Mon Oct 17, 2005 4:16 am
Posts: 4
You may notice that in the criteria for retrieving the recentPersons list (public void draw() ) I specifie the salary to be greater than 45,000 while the record is created with 35,000, however this is not the cause, as I've changed the value in the db and definitely have the Person come up, but not the associated Address.. which is the problem.

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 9:36 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
The problem is in the setter:

Code:
private IList addresses = new ArrayList();
public IList Addresses
{
  get { return addresses; }
  set { value = value; }
}

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject: Thanks !
PostPosted: Mon Oct 17, 2005 10:13 pm 
Newbie

Joined: Mon Oct 17, 2005 4:16 am
Posts: 4
Hi,

Thanks for that, I feel like a real dork. must have been the late night stints that finally did it to me..

Much appreciated.


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.