Hello everyone,
I have the following classes:
Code:
public class A
{
private int id;
private string description;
private C nameC;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public C NameC
{
get
{
return nameC;
}
set
{
nameC = value;
}
}
}
public class B:A
{}
public class C
{
private int id;
private string name;
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
Class B inherits class A, class C is associated to class B.
The above classes are mapped to the tables A(Id, Description, NameC), B(Id), C(Id, Name).
There is a many-to-one unidirectional association from class A to class C, and class B is mapped as a joined subclass of class A.
I am trying to retrieve a list of C instances (List<C>) using the following function:
Code:
public IList LoadAll(Type t)
{
ISession session = this.OpenSession();
try
{
BeginTransaction();
ICriteria criteria = session.CreateCriteria(t);
if (autoCommit)
CommitTransaction();
return criteria.List();
}
catch (HibernateException e)
{
throw new PersistenceException(String.Format("{0}", "Failed to load!"), e);
}
finally
{
if (autoCloseSession)
CloseSession();
}
, where Type t is C.
What I am receiving is a collection of class A instances. Within a instance of A, I have the properties Id, Description and the object NameC. The property Id and Description recibe the corresponding values from the Db. The object NameC have its properties Name with value, but the property Id has the value 0.
Why the property Id of the object NameC doesn't receive the value from the Db. The object C is accessed via its proxy, so it seems that the problem might be there.
I have browsed for a solution for this kind of problem but I did't manage to clarify it.
Has anyone faced this problem and how did you manage to solve it ?
Thank you,
C
------------------------------------
I believe that the behaviour above mentioned is caused by the fact that the A.NameC attribute is not being proxied / or the proxy is not initialized. That was strange, as I mapped all the classes with the Lazy="true" attribute. However I have managed to read correctly the A.NameC property, by reading its proxy:
Code:
var proxy = A.NameC as INHibernateProxy;
C correctC = (c)proxy.HibernateLazyInitializer.GetImplementation();
Anyway, does anyone know why the proxy has not been initialized ? Have I missed anything ?
Thank you,
C.