Search code examples
nhibernatecriteria

Disjunction between tables that always dont exist


I have mapped K and Other to a parent table Errand and they are related true an id, the thing is the Errand only has one K or one Other never both! When I have a Crieteria Query on only one row like this is works seamlessly;

    public void AddNoteQuery(DetachedCriteria query)
    {
        if (_request.Note != null)
        {
            query
                .CreateAlias("Other", "o")
                .Add(Restrictions.Eq("o.Note", _request.Note));
        }
    }

But when I try to do an K OR Other Criteria Query I get no results!

    public void AddNoteQuery(DetachedCriteria query)
    {
        if (_request.Note != null)
        {
            query
                .CreateAlias("K", "k")
                .CreateAlias("Other", "o")
                .Add(Restrictions.Or(
                    Restrictions.Eq("k.Note", _request.Note),
                    Restrictions.Eq("o.Note", _request.Note)));
        }
    }

Solution

  • Try specify the join type:

    query
        .CreateAlias("K", "k", JoinType.LeftOuterJoin)
        .CreateAlias("Other", "o", JoinType.LeftOuterJoin)
        .Add(Restrictions.Or(
            Restrictions.Eq("k.Note", _request.Note),
            Restrictions.Eq("o.Note", _request.Note)));