NHibernate criteria query question

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2009-11-24T14:09:56Z Indexed on 2010/03/21 2:01 UTC
Read the original article Hit count: 395

Filed under:

I have 3 related objects (Entry, GamePlay, Prize) and I'm trying to find the best way to query them for what I need using NHibernate. When a request comes in, I need to query the Entries table for a matching entry and, if found, get a) the latest game play along with the first game play that has a prize attached. Prize is a child of GamePlay and each Entry object has a GamePlays property (IList).

Currently, I'm working on a method that pulls the matching Entry and eagerly loads all game plays and associated prizes, but it seems wasteful to load all game plays just to find the latest one and any that contain a prize.

Right now, my query looks like this:

var entry = session.CreateCriteria<Entry>()
    .Add(Restrictions.Eq("Phone", phone))
    .AddOrder(Order.Desc("Created"))
    .SetFetchMode("GamePlays", FetchMode.Join)
    .SetMaxResults(1).UniqueResult<Entry>();

Two problems with this:

  1. It loads all game plays up front. With 365 days of data, this could easily balloon to 300k of data per query.
  2. It doesn't eagerly load the Prize child property for each game. Therefore, my code that loops through the GamePlays list looking for a non-null Prize must make a call to load each Prize property I check.

I'm not an nhibernate expert, but I know there has to be a better way to do this. Ideally, I'd like to do the following (pseudocode):

entry = findEntry(phoneNumber)
lastPlay = getLatestGamePlay(Entry)
firstWinningPlay = getFirstWinningGamePlay(Entry)

The end result of course is that I have the entry details, the latest game play, and the first winning game play. The catch is that I want to do this in as few database calls as possible, otherwise I'd just execute 3 separate queries.

The object definitions look like:

public class Entry 
{
    public Guid Id {get;set;}
    public string Phone {get;set;}
    public IList<GamePlay> GamePlays {get;set;}
    // ... other properties
}

public class GamePlay 
{
    public Guid Id {get;set;}
    public Entry Entry {get;set;}
    public Prize Prize {get;set;}
    // ... other properties
}

public class Prize
{
    public Guid Id {get;set;}
    // ... other properties
}

The proper NHibernate mappings are in place, so I just need help figuring out how to set up the criteria query (not looking for HQL, don't use it).

© Stack Overflow or respective owner

Related posts about nhibernate