0

我有两个课程(只是重现问题):

public class User
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual IList<OrgUnitMembership> OrgUnitMemberships { get; set; }
}

public class OrgUnitMembership
{
    public virtual int UserId { get; set; }
    public virtual int OrgUnitId { get; set; }
    public virtual DateTime JoinDate { get; set; }
    public virtual DateTime LeaveDate { get; set; }
}

当然,两者都有一个 Fluent NHibernate 地图:

    public class UserMapping : ClassMap<User>
    {
        public UserMapping()
        {
            Table("Users");

            Id(e => e.Id).GeneratedBy.Identity();
            Map(e => e.FirstName);
            Map(e => e.LastName);

            HasMany(x => x.OrgUnitMemberships)
.KeyColumn(TypeReflector<OrgUnitMembership>
.GetPropertyName(p => p.UserId))).ReadOnly().Inverse();
        }
    }
    public class OrgUnitMembershipMapping : ClassMap<OrgUnitMembership>
    {
        public OrgUnitMembershipMapping()
        {
            Table("OrgUnitMembership");

            CompositeId()
                .KeyProperty(x=>x.UserId)
                .KeyProperty(x=>x.OrgUnitId);

            Map(x => x.JoinDate);
            Map(x => x.LeaveDate);

            References(oum => oum.OrgUnit)
.Column(TypeReflector<OrgUnitMembership>
.GetPropertyName(oum => oum.OrgUnitId)).ReadOnly();
            References(oum => oum.User)
.Column(TypeReflector<OrgUnitMembership>
.GetPropertyName(oum => oum.UserId)).ReadOnly();
        }
    }

我想做的是根据条件检索一些用户,但我想将 Users 表中的所有列与 OrgUnitMemberships 表中的一些列组合起来,类似于 SQL 查询:

select u.*, m.JoinDate, m.LeaveDate
from Users u inner join OrgUnitMemberships m on u.Id = m.UserId
where m.OrgUnitId = :ouid

我完全迷失了,我尝试了许多不同的选择。使用普通的 SQL 查询几乎可以工作,但是由于 User 类 AliasToBean 中有一些可为空的枚举无法转换,否则包装 SQL 查询将像这样工作:

return
    Session
        .CreateSQLQuery(sql)
        .SetParameter("ouid", orgUnitId)
        .SetResultTransformer(Transformers.AliasToBean<UserDTO>())
        .List<UserDTO>()

我尝试了下面的代码作为测试(一些不同的变体),但我不确定我在做什么。它部分工作,我得到了 UserDTO 的实例,来自 OrgUnitMembership 的属性(日期)已填充,但来自 User 的所有属性都为空:

User user = null;
                OrgUnitMembership membership = null;
                UserDTO dto = null;
                var users = Session.QueryOver(() => user)
                    .SelectList(list => list
                        .Select(() => user.Id)
                        .Select(() => user.FirstName)
                        .Select(() => user.LastName))
                    .JoinAlias(u => u.OrgUnitMemberships, () => membership)
                    //.JoinQueryOver<OrgUnitMembership>(u => u.OrgUnitMemberships)
                    .SelectList(list => list
                        .Select(() => membership.JoinDate).WithAlias(() => dto.JoinDate)
                        .Select(() => membership.LeaveDate).WithAlias(() => dto.LeaveDate))
                    .TransformUsing(Transformers.AliasToBean<UserDTO>())
                    .List<UserDTO>();
4

1 回答 1

1

如此接近.. 只需加入您的 2 个 SelectList 方法 - 您的别名将确保 NHibernate 从正确的实体获取正确的属性:

User user = null;
OrgUnitMembership membership = null;
UserDTO dto = null;
var users = Session.QueryOver(() => user)
    .JoinAlias(u => u.OrgUnitMemberships, () => membership)
    //.JoinQueryOver<OrgUnitMembership>(u => u.OrgUnitMemberships)
    .SelectList(list => list
        .Select(() => user.Id)
        .Select(() => user.FirstName)
        .Select(() => user.LastName))
        .Select(() => membership.JoinDate).WithAlias(() => dto.JoinDate)
        .Select(() => membership.LeaveDate).WithAlias(() => dto.LeaveDate))
    .TransformUsing(Transformers.AliasToBean<UserDTO>())
    .List<UserDTO>();
于 2012-10-12T15:59:20.093 回答