当只映射一侧时,查询多对多nhibernate

时间:2010-09-14 16:36:56

标签: nhibernate many-to-many icriteria

我有以下实体

public class Client
{
    public virtual int Id{get;set;}
    public virtual IList<Telephone> Telephones { get; private set; }
}

public class User
{
    public virtual int Id{get;set;}
    public virtual IList<Telephone> Telephones { get; private set; }
}

public class Telephone
{
    public virtual int Id{get;set;}
    public virtual string Number { get; set; }
    public virtual string Extension { get; set; }
    public virtual TelephoneType TelephoneType { get; set; }
}

客户端就像这样的映射

HasManyToMany<Telephone>(x => x.Telephones)
.Table("tblClientTel")
.ParentKeyColumn("ClientId")
.ChildKeyColumn("TelId")
.LazyLoad()
.Cascade.SaveUpdate();

用户像这样的映射

HasManyToMany<Telephone>(x => x.Telephones)
.Table("tblUserTel")
.ParentKeyColumn("UserId")
.ChildKeyColumn("TelId")
.LazyLoad()
.Cascade.SaveUpdate();

和这样的电话

   public TelephoneMap()
    {
        Table("tblTel");
        Id(x => x.Id, "Id");
        LazyLoad();
        References<TelephoneType>(x => x.TelephoneType, "TypeId")
            .Not.Nullable()
            .Cascade.None()
            .Not.LazyLoad();
        Map(x => x.Number, "Number")
            .Not.Nullable()
            .Length(15);
        Map(x => x.Extension, "Extension")
            .Nullable()
            .Length(10);
    }

如何查询客户列表中的所有电话实体? 我试过这个

ICriteria criteria = base.CreateCriteria<Client>(null);
return base.CreateCriteria
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Telephones"))
)
.Add(Expression.In("Id", clientIds))
.SetFetchMode("Telephones", FetchMode.Eager)
.List<Telephone>();

但它返回客户端ID

0 个答案:

没有答案