Search code examples
nhibernateicriteria

Querying Overriding Entities Using a Self Join and the NHibernate Criteria API


I have a simple Waiver model, and I would like to make a query that returns all the Waivers that are not overridden.

public class Waiver
{
    private readonly int id;

    protected Waiver()
    {
        this.id = 0;
    }

    public virtual int Id { get { return id; } }

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual bool IsRequired { get; set; }
    public virtual DateTime EffectiveDate { get; set; }

    public virtual Waiver OverriddenWaiver { get; set; }
}

Here's the map:

  <class name="Waiver" table="Music_Waivers">
    <id name="id" access="field" column="WaiverId" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" />
    <property name="Description" column="Description" />
    <property name="IsRequired" column="IsRequired" />
    <property name="EffectiveDate" column="EffectiveDate" />

    <many-to-one name="OverriddenWaiver" class="Waiver" column="OverrideWaiverId" />
  </class>

Now I want to have a method in my Repository with the signature public IList GetLatest(). For some reason I'm having a hard time implementing this with the CriteriaAPI. I can write this in T-SQL no problem.


Solution

  • I ended up brute forcing a solution. It's not pretty, but since I know the table will be tiny (probably going to end up being only 5 rows) I came up with the following code solution:

    public IList<Waiver> GetLatest()
    {
        using (var session = SessionManager.OpenSession())
        {               
            var criteria = session.CreateCriteria(typeof (Waiver));
            var waivers = criteria.List<Waiver>();
    
            var nonOverridenWaivers = new List<Waiver>();
    
            foreach(var waiver in waivers)
            {
                bool overrideExists = waivers.Any(w => w.Overrides != null &&
                                                       w.Overrides.Id == waiver.Id);
                if (!overrideExists)
                    nonOverridenWaivers.Add(waiver);
            }
    
            return nonOverridenWaivers;
        }
    }