-->
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.  [ 3 posts ] 
Author Message
 Post subject: can't access fields directly when lazy loading is turned on.
PostPosted: Sun Jul 29, 2007 4:11 pm 
Newbie

Joined: Sun Jul 29, 2007 3:34 pm
Posts: 3
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;
}
.............
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 2:42 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
I don't believe you can proxy fields, only methods/properties. The properties must be marked virtual and you must provide at least a protected default .ctor.
Mike

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 5:20 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi!

You may use ISession.Get() instead of ISession.Load() - it will try to return "real object" insead of proxy (but it may return proxy, if this object was already requested before via lazy association).
The other important point - you should "initialize" proxy before trying to access fields, as before "initialization" they definetely will be "empty" - after initialization they will be filled from DB. Proxy is a special subclass of your POCOs - it intercept almost all object access (and initialize proxy before proceeding with your "method call/property accessor call") - but it can't intercept direct field access of course.

Simple way to initialize "possible" proxy (it won't throw exception if you pass real object - the one that don't need any special initialization) is NHibernateUtil.Initialize(object possibleProxyOrCollection) method.

_________________
WBR, Igor


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.