+3  A: 

Im not supprised this doesn't feel right to you, it not. Infact it's so wrong it hurts!But instead of just just giving you grief ill explain all the reason why this is wrong@

Firstly, your load in ALL the trips into memory from the database, this essentially means you are querying a data store to get a whole load of data that you don't need and then passing it down the wire. This is fine if you have a few trips, but this will not scale at all.

Next you are recursing through each trip and calling the property "trip.People". This will then hit the database again and load ALL the data for people on EACH one of these trips. Again, this will kill you if you have several trips with several attendees. Now, this is assuming that you have no filters on your mappings or you have specifically asked NHibernate not to lazy load your People collection, but either way that's a whole load of data you don't want.

My advice would be to look at the NHibernate documents concerning querying your object model using either HQL or Linq-To-NHibernate, and you'll end up with queries that look something like the following:

HQL (MY HQL Sucks so this could be very wrong):

var hql = @"from Trip as t join t.People as p with p.Account.Id = :accountId select t"

Edit:

Actually, my brain is a little slow right now as its late, but I just realized that your doing things a little backwards here. What your actually after is all the trips that an person has been on, so why doesn't your account object have a Trips collection mapped to is? You should essentially be aiming for something like this:

var trips = accountRepo.GetAccount(123).Trips;

Edit:

Again I am tired so this may be nonsense, but I think the mapping you are looking for will look like this:

 <bag name="Trip"  cascade="all" table="TripToAccount" lazy="true">
  <key column="AccountId" />
  <many-to-many class="Trip">
   <column name="TripId" not-null="true"/>
  </many-to-many>
 </bag>

Edit:

Damn it, i should go to bed. Now I see you already have a mapping between the people and their trips so why not:

var query = "from TripPeople as tp Where tp.Account.Id = :accountId AND tp.IsActive = true select tp.Trip"

Going to stop answering now, before I do more stupid things.

Owen
Hi, thanks for the comment. Yeah this code stinks pretty bad but I'm a noob to NHibernate and programming good OO design.:P Yeah I totally agree that what I'm after is something like var trips = myAccount.Trips but I'm really stuck on thinking this one through. Maybe the answer is in front of me but I'm a total idiot. I'm trying to figure out how I would map the Trip list in Account to data in the TripPeople aggregate. I need to think about this for a while...
CalebHC
Assuming you have something like these tables: Trip, TripToAccount, Account then the mapping in the example will work for you (I think).
Owen
The only problem I'm running into is that my TripPeople table is an entity because it needs a state.
CalebHC
Yeah, i finally realized that in my last edit (doh!). Would the HQL I posted not work, or are you trying to achieve this via a property mapping? If your trying to add it as a mapping, can you post what you have so far?
Owen
A: 

What Owen said could be the best way to do it if you didn't load your trips before you hit this method.

What I suggest would be to build some kind of compare() method for a class List of trips

public class Trips: List< Trip > {

public Trips()
{
    //
    // TODO :
    //
}


#region methods

/// <summary>
/// return trip
/// </summary>

public Trip FindTrip(int accountId)
{
    return this.Find(delegate(trip t) {
    return t.AccountId == accountId; });
}
#end region

}

I can't be sure if it's the best but that's the way I do it hahan hahan it's like the Find() method of the object class

A: 
CalebHC
Looks much more sensible :-)
Owen