这个问题与此处的问题很接近,但并不相同:
NHibernate Query across multiple tables
基本上,我的问题给出以下模型,我将如何查询以查找当前狗的名称为“ foo”还是过去的狗的名称为“ foo”(析取)。本质上,我对CurrentDog具有多对一关系,对于PastDogs具有多对多关系。
public class Dog {
public string name {get; set;}
}
public class Owner {
public string firstname {get; set;}
public string lastname {get; set;}
public Dog CurrentDog {get; set;}
public Dog[] PastDogs {get; set;}
}
我猜想SQL应该看起来像这样:
SELECT o.* FROM owners AS o
INNER JOIN dogs AS cd ON o.current_dog_id = cd.id
INNER JOIN owner_past_dog_maps AS pd ON o.id = pd.owner_id
INNER JOIN dogs AS d ON pd.dog_id = d.id
WHERE d.name = 'foo'
OR cd.name = 'foo'
希望有道理...如果有人问,我会尽力澄清。
最佳答案
我尝试使用QueryOver和别名解决它
Owner myOwner = null;
Dog myCurrentDog = null;
Dog myPastDogs = null;
var sax = _HibSession.QueryOver<Owner>(() => myOwner)
.JoinAlias(() => myOwner.CurrentDog, () => myCurrentDog, JoinType.InnerJoin)
.JoinAlias(() => myOwner.PastDogs, () => myPastDogs , JoinType.InnerJoin)
.Where(Restrictions.Disjunction()
.Add(Restrictions.EqProperty(myCurrentDog.Name,"foo"))
.Add(Restrictions.EqProperty(myPastDogs.Name,"foo"))
)
.List<Owner>();
希望对您有所帮助!
关于c# - 跨多对多和多对多关系的Nhibernate析取(“OR”)查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7169441/