NHibernate QueryOver how to join on non declared relationship

后端 未结 2 609
囚心锁ツ
囚心锁ツ 2020-12-09 13:41

How to do the following join to return Users who have access to a Company given a company id. The problem is there is no explicit relationship using a User object between Us

2条回答
  •  温柔的废话
    2020-12-09 14:05

    As of 5.1.0, it is possible to make hibernate generate an actual sql join on an undeclared (unmapped) relationship. E.g. all orders sorted by customer's spending:

    var criteria = _session
        .CreateCriteria("order");
    
    criteria
        .CreateEntityAlias(
            "customer",
            Restrictions.EqProperty("order.customerId", "customer._id"),
            JoinType.LeftOuterJoin,
            typeof(Customer).FullName)
        .AddOrder(new Order("customer._lifetimeSpending", ascending:false));
    
    return criteria.List();
    

    Also possible with QueryOver (sample from NHibernate docs):

    Cat cat = null;
    Cat joinedCat = null;
    
    var uniquelyNamedCats = sess.QueryOver(() => cat)
        .JoinEntityAlias(
            () => joinedCat,
            () => cat.Name == joinedCat.Name && cat.Id != joinedCat.Id,
            JoinType.LeftOuterJoin)
        .Where(() => joinedCat.Id == null)
        .List();
    

提交回复
热议问题