Hi Thank you very much for your experties.
First off, with lazy loading disabled, I experience no problem.
When i enable lazy loading, I can't seem to access private fields directly. I tried mapping with both the property way:
<id name="Id" access="nosetter.camelcase-underscore" type="Guid">
<generator class="assigned"/>
</id>
and the field way:
<id name="_id" column="Id" access="field" type="Guid">
<generator class="assigned"/>
</id>
And in both cases, when I try to access private field in the same class's static method, I get a 0. I don't get exception or any error. It just gives me a 0 (default value of GUID).
The correct value (retreived from DB) is only shown if I use the getter property.
Also, writing into field is the same thing. The value only gets persisted if I write into a setter property, not when I use field directly.
Why is this happening? When NHibernate populates my object lazily, shouldn't it populate the field too?
There must be something I am missing....
public class DbUser : BaseNHibernateClass<DbUser, Guid>
{
private Guid _id;
private string _name;
private Profile _info;
public virtual Guid Id {
get { return _id; }
}
public virtual string Name {
get { return _name; }
set { _name = value; }
}
public static void ConfirmUser(Guid id, string name, Sex sex, DateTime birthdate)
{
DbUser me = NHibernateSession().Load(typeof(DbUser), id);
string k = me._id.ToString(); // does not work. i.e. k = "0000.. me._name = name; // does not work. _name is not persisted when SaveOrUpdate() is called.
string p = me.Id.ToString(); // works. me.Name = name; // works. Name is persisted when SaveOrUpdate() is called.
me._info = Profile.Create(id, sex, birthdate);
NH.NHibernateSession().SaveOrUpdate(me);
}
.................
}
public class NH
{
/// <summary>
/// Exposes the ISession used within the DAO.
/// </summary>
private static ISession NHibernateSession() {
session = sessionFactory.OpenSession()
return session;
}
.............
}
|