views:

36

answers:

0

I have a use-case in my app where I need the first two levels of my object tree. I have an object called Player which has a list called CheckIns that holds a many-to-one connection to Player as an object reference

when I execute the Query I get back a seemingly endless chain of Player->CheckIns->Player->CheckIns and so on..

What I need is Player->CheckIns

in other use-cases I fetch the CheckIns (Child object) first and then I need a reference to his parent.. that's why I have this reference.

the relevant code :

public ICriteria AddFetchJoinTo(ICriteria criteria)
{
   criteria.SetFetchMode("PlayerCheckIns", FetchMode.Join)
                .SetFetchMode("PlayerCheckIns.Player",  FetchMode.Lazy);

   return criteria;
}

public IList<T> RetrieveEquals<T>(string propertyName, object propertyValue, IFetchingStrategy fetchingStrategy)
{
  using (ISession session = m_SessionFactory.OpenSession())
  {
        ICriteria criteria = session.CreateCriteria(typeof(T));
        if (fetchingStrategy != null)
        {
            fetchingStrategy.AddFetchJoinTo(criteria);                        
        }
            criteria.Add(Expression.Eq(propertyName, propertyValue));
            // Get the matching objects
            IList<T> matchingObjects = criteria.List<T>();                

            // Set return value
            return matchingObjects;         
        }           
}

Player.hbm.xml

<bag name="PlayerCheckIns" cascade="all-delete-orphan"       lazy="true" >
  <key column="PlayerID" />
  <one-to-many class="CheckIn" />
</bag>

CheckIn.hbm.xml

<!-- Identity mapping -->
<id name="ID">
  <column name="ID" />
  <generator class="native" />
</id>

<!-- Properties-->    

<many-to-one name="Player"  class="Player" column="playerId" />

thanks alot! Igal