我有一个看起来很直接的nHibernate查询问题,但我似乎无法理解它!
我正在创建一些简单的示例类来说明我的问题:
public class Car {
public int Id { get; set; }
public IList<Interior> InteriorParts { get; set; }
}
public class Interior {
public int Id { get; set; }
public InteriorProducer Producer { get; set; }
}
public class InteriorProducer {
public int Id { get; set; }
}
现在查询:我有InteriorProducer的id,但是需要获得内部制作人已经制作内部的汽车列表。
因此,在一个简单的伪SQL中,它看起来像这样:
select cars
where car.InteriorParts.Producer.Id = Id
我很难解决这个问题,以创建一个nHibernate查询。
任何想法?
由于
答案 0 :(得分:3)
var cars = session
.CreateCriteria<Car>()
.CreateAlias("InteriorParts", "parts")
.CreateAlias("parts.Producer", "producer")
.Add(Expression.Eq("producer.Id", id))
.List();
根据您的情况,您可能还希望过滤由于SQL连接而将被提取的重复的汽车:
.SetResultTransformer(Transformers.DistinctRootEntity)
答案 1 :(得分:2)
并在hql ...
var query = session.CreateQuery
("select c from Cars c where c.InteriorParts.Producer.Id = :pid");
query.SetInt32("pid", producerId);
IList<Car> cars = query.List<Car>();