English 中文(简体)
QueryOver——如何从三张表格中只选一个物体
原标题:QueryOver - How to select only one object from a join of three tables

假设我有类似目标:

public class User
{
    public int UserId { get; set; } 
    public IList<UsersRentingLog> UsersRentLog { get; set; }
}

public class Car 
{
    public int CarId { get; set; } 
    public IList<UsersRentingLog> CarRentLog { get; set; }
}

public class UsersRentingLog
{
    public Userid { get; set; }
    public CarId { get; set; }
}

现在,我要选择租用一辆汽车的所有使用者。

这样做只会是 The。

select c.*
from [User] u
    inner join UsersRentingLog l on u.userid = l.UserId
    inner join Car c on l.CarId = c.CarId
where u.userid = @UserId

我正试图通过这一询问在NHibernate的QueryOver工作。

DetachedCriteria dc = QueryOver.of<User>()
    .where(r => r.UserId == userId)
    .JoinQueryOver<UsersRentingLog>(l => l.UsersRentLog)
    .JoinQueryOver<Car>(c => c.Car)
    .DetachedCriteria
    ;

This is selecting, as expected, every single property from all three joined tables, but I actually want to select only Cars. How can I do this please?


Update on correct answer

因此,在答复之后,我不得不作几处改动(大多数是小的辛迪加错误),认为我把工作文本放在了后面。

由于我的使用人没有直接提及汽车,我不得不修改其他内容来这样做。

UsersRentingLog logAlias = null; 
var subQuery = QueryOver.of<User>()
    .Where(user => user.UserId == userId)
    .JoinAlias(user => user.UsersRentLog, () => logAlias)
    .subQuery.Select(Projections.Distinct(Projections.Property(() => logAlias.Car.Id)));


var query = _session.QueryOver<Car>();
    query.WithSubquery.WhereProperty(car => car.Id).In(subQuery)
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Car>();

以上一点,我使用的是过时的标准,因此,我改写了第二组法典,如:

DetachedCriteria dc = QueryOver.Of<Car>()
    .WithSubquery.WhereProperty(car => car.Id).In(subQuery)
    .TransformUsing(new NHibernate.Transform.DistinctRootEntityResultTransformer())
    .DetachedCriteria
    ;
最佳回答
Car carAlias = null;
var subQuery = QueryOver.of<User>()
    .Where(user => user.UserId == userId)
    .JoinAlias(user => user.Cars, () => carAlias)
    subQuery.Select(Projections.Distinct(
      Projections.Property(() => carAlias.Id)));

var query = _session.QueryOver<Car>();
query.WithSubquery.WhereProperty(car => car.Id).In(subQuery);
.TransformUsing(Transformers.DistinctRootEntity)
.List<Car>();
问题回答

暂无回答




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签