NHibernate的查询寻找相关对象的相关对象对象、NHibernate

2023-09-06 06:33:39 作者:遗心遗梦遗幸福

我有一个NHibernate querie问题,看起来挺简单的,但我似乎无法让我的头周围!

我创造了一些简单的示例类来说明我的问题:

 公共类车{
    公众诠释编号{获得;组; }
    公众的IList<室内装饰> InteriorParts {获得;组; }
}

公共类内部{
    公众诠释编号{获得;组; }
    公共InteriorProducer生产者{获得;组; }
}

公共类InteriorProducer {
    公众诠释编号{获得;组; }
}
 

现在到查询:我有InteriorProducer的ID,但需要获得其中内部已经产生了内部生产汽车的名单

因此​​,在一个简单的,伪SQL,它看起来是这样的:

 选车
其中,car.InteriorParts.Producer.Id = ID
 

我有一个很艰难的时期让我的头,围绕它来创建一个NHibernate查询。

任何想法?

感谢

解决方案

  VAR轿车=会议
    .CreateCriteria<车>()
    .CreateAlias​​(InteriorParts,份)
    .CreateAlias​​(parts.Producer,制片人)
    。新增(前pression.Eq(producer.Id,ID))
    的.List();
 
如何查询是否被录取 安排

根据你的情况,你可能还需要过滤,将因SQL中获取重复车加入:

  .SetResultTransformer(Transformers.DistinctRootEntity)
 

I have an nHibernate querie issue that looks quite straight forward, but I can't seem to get my head around it!

I am creating some simple example classes to illustrate my problem:

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; }
}

Now to the query: I have the id of the InteriorProducer, but need to get a list of Cars where the interior have been produced by the interior producer.

So in a simple, pseudo SQL, it looks something like this:

select cars
where car.InteriorParts.Producer.Id = Id

I have a really hard time getting my head around this to create an nHibernate query.

Any Ideas?

Thanks

解决方案

var cars = session
    .CreateCriteria<Car>()
    .CreateAlias("InteriorParts", "parts")
    .CreateAlias("parts.Producer", "producer")
    .Add(Expression.Eq("producer.Id", id))
    .List();

Depending on your case you might also want to filter duplicate cars that will be fetched due to the SQL joins:

.SetResultTransformer(Transformers.DistinctRootEntity)