-->
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.  [ 10 posts ] 
Author Message
 Post subject: Lazy issues with Load<>() is it me or am I going mad!!
PostPosted: Mon Sep 24, 2007 12:26 pm 
Newbie

Joined: Fri Jul 28, 2006 10:11 am
Posts: 7
Location: Nottinghamshire, England
Hibernate version:1.2.0.GA

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OS.HolidayAbsenceManagement.Core" namespace="OS.HolidayAbsenceManagement.Core" default-access="field">
<class name="Team" table="team">
<id name="id" unsaved-value="0">
<generator class="native" />
</id>
<property name="name" type="string" length="30" unique="true" not-null="true"/>
</class>
</hibernate-mapping>

I have the following unit test:
[Test]
public void Mapping_works_for_Team()
{
string teamName = "Team1";

ITeam team = new Team(teamName);

ISession session = new NhibernateSessionManager(new SessionContext()).GetSessionFrom(CONFIG_FILE);
session.Save(team);
session.Flush();
session.Evict(team);

Assert.AreNotEqual(0, team.Id, "Didn't assign an ID to saved team");
ITeam persistedTeam = session.Load<Team>(team.Id);

Assert.AreEqual(team.Name, persistedTeam.Name);
}
}



SQL Express 2005


Hi when I run the above unit test with lazy load turned on, none of my object properties are poplutated with info from the database.

As soon as I turn lazy off, the properties are populated... HELP !!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 24, 2007 12:53 pm 
Newbie

Joined: Fri Jul 28, 2006 10:11 am
Posts: 7
Location: Nottinghamshire, England
hmmm...

I have just been playing and it appears that Load only retrieves items from the catch :S if lazy load is enabled.

Is this the 'expected behaviour' of Load() and if so... why?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 24, 2007 2:41 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
I've had issues with the Load method before

Try using the Get<>() method. I'm not exactly sure the difference between the two.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 24, 2007 11:22 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Please read the documentation here
[url]
http://www.hibernate.org/hib_docs/nhibe ... ta-loading
[/url]

for the difference between Load and Get. This should clear up any confusion.

Thanks
MIKE

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 2:57 pm 
Newbie

Joined: Fri Jul 28, 2006 10:11 am
Posts: 7
Location: Nottinghamshire, England
Hi,

Thanks for the pointers to the documentation, I have already read it before, but have just reread it.

According to the docs my test should have passed... But it did not.

Over the last few months I have been using the Get<> method to retrieve instances from the object store.

But now I need to start utilising lazy loading and I am hitting the same problem that I originally asked about :(

I really could do with some help or a valid example that uses the get<> method.

Again, thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 1:51 am 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
I use get all the time, I prefer the null to an exception.

If your using lazy loading, you have to make sure the property is initialized before you access it, or keep the session open when you do access the fields. I use Spring.net and it wont keep the sessions alive.

So to check to see if a property has been loaded:
NhibernateUtil.IsInitialized(obj.property)

if it is false you have to reconnect to a session using a refresh command or update or whatever to reconnect to the object to the session.

Then use
NhibernateUtil.Initialize(obj.property) to load the component, list or whatever that is currently in the lazy state.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 7:19 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Mad as a hatter, sorry. :)

In a lazy loading scenario Load gets an item from the cache if it exists (it doesn't in your case - you evicted it), otherwise it creates a proxy of the object and doesn't touch the DB unless you attempt to access any of the object's members (other than the Id property and GetHash/Equals I think).

This can be useful if you need to refer to another object that exists in the DB but you don't want to load it - for example, if you have an Address object in the DB and you want to set it as the address of a new User object you don't have to read the Address object info from the DB, just create a proxy to the Address object using Load and add it to the User object. When you save the User object it will put the id of the Address object into the address field of the user table.

If the object for the loaded proxy doesn't exist you'll get an exception. Also expected behavior.

Get, on the other hand with Get the DB is touched immediately. If the object doesn't exist you get null.

Hope that helps.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 2:28 pm 
Newbie

Joined: Fri Jul 28, 2006 10:11 am
Posts: 7
Location: Nottinghamshire, England
merge_s.rottem:
Thanks for your informative explanation of how load works :) which I already understand, and if you where to look at my test, I am actually testing that the name property of the team object is correct and should have deed touched the db to collect the information at that point (it’s not).
padlewski:
Again... Thanks for your comment, but as you can see from the test case that I supplied, the original session that retrieves the team object is still active when I assert against the Name property of the team instance.

Interestingly… when I turn lazy off for the team mapping it’s list of members are still loaded :P


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 5:45 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Sorry about that...can we see you class code please?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 5:05 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
You might also want to set ShowSql to true. The actual query may help find the problem.


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