-->
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: How to stop NH reloading an item
PostPosted: Wed May 25, 2005 10:06 pm 
Newbie

Joined: Sun May 15, 2005 11:39 pm
Posts: 17
Location: Australia
Hello all,

I am using NH to load up ILists (Bags) with items.

I was hoping to make one call on a session to have everything load in a single database trip and then query the session object to return individual items (without making another db call).

I can't seem to get this to happen.

The code I'm using to load 'everything' is

Code:
IList result = Session.CreateCriteria(typeof(clsItem)).List();


I am then calling the following for individual items

Code:
IList i = this.DataBinder.Session.Find("from clsItem i where i.Litm = '" + Litm + "'");

Note - the Session object is the same and it hasn't been closed or disconnected.

If it helps, here is the map for clsItem...

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="OPWLibrary.clsItem, OPWLibrary" table="tblItems">
      <id name="Litm" column="cLITM" unsaved-value="0" length="6" type="string">
         <generator class="assigned" />
      </id>
            
      <property name="Description" column="cDescription"/>
      <property name="ShelfLifeDays" column="iShelfLifeDays"/>
   
   <bag name="OrdersNH" inverse="true" lazy="true">
      <key column="cLITM"/>
      <one-to-many class="OPWLibrary.clsOrder,OPWLibrary"/>
   </bag>

   </class>
</hibernate-mapping>


Ideally, I guess that I would want to use a MAP/IDictionary to achieve this, however the collection has to participate in a bi-directional relationship, so (as I understand it) I'm restricted to bags and maps.

I'd be very appreciative if someone could give me some advice on how to tackle this issue. I've thought of putting a function in my collection wrapper that loops through the IList and returns a result... Or maybe even populate it into a Hashtable on construction... Are these the best ways to tackle this, or, Is there a way to return the object from the NH Session?

Thanks very much in advance,




Damien Sawyer


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 4:14 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
If I understood right, you wanted to preload with the CreateCriteria method and then retrieve the objects from cache on each Find call.

For NHibernate any query created with CreateCriteria, Find, CreateQuery, CreateSqlQuery transforms in a round-trip on the DB as it is no possibility for it to be sure that no new objects satisfying the search condition.

You can use Session.Get or Session.Load to obtain the object from the cache as both methods search in first level cache, the second level cache and the hit the db, but, these methods search by ID.

If the field you are searching by is the ID than you can use the Get or Load methods, otherwise you should find another approach on this, maybe caching by yourself the collection and than searching in it on the business layer.

If I understood right, you wanted to preload with the CreateCriteria method and then retrieve the objects from cache on each Find call.

For NHibernate any query created with CreateCriteria, Find, CreateQuery, CreateSqlQuery transforms in a round-trip on the DB as it is no possibility for it to be sure that no new objects satisfying the search condition.

You can use Session.Get or Session.Load to obtain the object from the cache as both methods search in first level cache, the second level cache and the hit the db, but, these methods search by ID.

If the field you are searching by is the ID than you can use the Get or Load methods, otherwise you should find another approach on this, maybe caching by yourself the collection and than searching in it on the business layer.

So use: Session.Get( typeof(clsItem), someLitmValue )

Good luck,
Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 1:05 pm 
Newbie

Joined: Sun May 15, 2005 11:39 pm
Posts: 17
Location: Australia
Thanks Dragos,

That's a big help. I have to say, after a rough start, I'm finally starting to get the idea of NH. It really is fantastic! :-)


DS


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.