Allright, I set up a sample application considering another problem, but I quickly wrote a testcase for this issue too, here are the files:
mapping file:
Child:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DaoLayer.Entity.Child, DaoLayer" table="children">
<id name="ID" column="childId" type="String">
<generator class="uuid.hex" />
</id>
<property name="Name" column="name" type="string" />
<many-to-one name="Parent" column="aId" class="DaoLayer.Entity.A, DaoLayer"/>
</class>
</hibernate-mapping>
Parent:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DaoLayer.Entity.A, DaoLayer" table="entities">
<id name="ID" column="aId" type="String">
<generator class="uuid.hex" />
</id>
<set name="Children" cascade="all-delete-orphan">
<key column="aId" />
<one-to-many class="DaoLayer.Entity.Child, DaoLayer" />
</set>
</class>
</hibernate-mapping>
Testcase:
Code:
[TestMethod()]
public void ProxyFetchingIDAvailableTest()
{
IDaoObject daoobj = (IDaoObject) ContextRegistry.GetContext().GetObject("DaoObject");
IObjectFactory objfac = (IObjectFactory)ContextRegistry.GetContext().GetObject("ObjectFactory");
IA a = objfac.CreateA();
for (int i = 0; i < 1; i++)
{
IChild child = objfac.CreateChild();
child.Name = "test" + i.ToString();
a.Children.Add(child);
}
IA a2 = (IA)daoobj.Save(a);
// load a child and check if a2.id can be retrieved
IChild persistedChild = objfac.CreateChild();
foreach (IChild child in a2.Children)
{
daoobj.LoadObjectById(persistedChild, child.ID);
Assert.IsNotNull(persistedChild.Parent.ID);
}
}
this testcase failes, b/c parent.Id is always null!
Its a very simple example it it doens't work. I'd really like to know if this is supposed to work with nhibernate!
I retrieve child with this class:
Code:
public class DaoObject : HibernateDaoSupport, IDaoObject
{
public void LoadObjectById(object obj, string _id)
{
try
{
this.HibernateTemplate.Load(obj, _id);
}
catch (Exception)
{
throw;
}
}
}
thx in advance n' regards!