[英]NHibernate Projections failure
我有與文章類有多對多關系的標簽類,問題是我想制作一個在標簽視圖模型類中表示的投影,所以列結果應該像這樣
Id|Name|CreatedBy|CreatedDate|LastModifiedBy|LastModifiedDate|ArticlesCount
SQL查詢應該是這樣的:
SELECT t.*, COUNT(at.intTag) as ArticlesCount
FROM dbo.TAG t
LEFT OUTER JOIN dbo.ARTICLE_TAG at ON t.intID = at.intTag
LEFT OUTER JOIN dbo.ARTICLE a ON at.intID = a.intID
GROUP BY t.intID, t.vcName, t.intWeight, t.vcCreatedBy, t.dtCreated, t.vcLastMod, t.dtLastMod, t.btActive
但它說
NHibernate.Exceptions.GenericADOException was caught
HResult=-2146232832
Message=could not execute query [ SELECT this_.intID as y0_, this_.vcName as y1_, this_.vcCreatedBy as y2_, this_.dtCreated as y3_, this_.vcLastMod as y4_, this_.dtLastMod as y5_, count(this_.intID) as y6_ FROM TB_TAG this_ WHERE this_.btActive = @p0 ]
Name:cp0 - Value:True
這是我的Tag類:
public class Tag {
public virtual string Name { get; set; }
public virtual int Weight { get; set; }
public virtual IList<Article> Articles { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string LastModifiedBy { get; set; }
public virtual DateTime LastModifiedDate { get; set; }
public virtual bool IsActive { get; set; }
}
這是映射類
internal sealed class TagMap : ClassMap<Tag>
{
public TagMap()
{
Table("TB_TAG");
Id(f => f.Id).Column("intID").GeneratedBy.Native();
Map(f => f.Name).Column("vcName").Not.Nullable();
Map(f => f.Weight).Column("intWeight").Not.Nullable();
Map(f => f.IsActive).Column("btActive");
Map(f => f.CreatedBy).Column("vcCreatedBy").Not.Update();
Map(f => f.CreatedDate).Column("dtCreated").Not.Update();
Map(f => f.LastModifiedBy).Column("vcLastMod");
Version(f => f.LastModifiedDate).Column("dtLastMod");
HasManyToMany(f => f.Articles).Table("S_ARTICLE_TAG")
.ParentKeyColumn("intTag").ChildKeyColumn("intID")
.Inverse();
}
}
這是ViewModel類:
public class TagView
{
public int Id { get; set; }
public string Name { get; set; }
public int ArticlesCount { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string LastModifiedBy { get; set; }
public virtual DateTime LastModifiedDate { get; set; }
public virtual bool IsActive { get; set; }
}
這是我的代碼進行投影:
Tag t = null;
tags = _session.QueryOver(() => t)
.Select(Projections.Id().As("Id"),
Projections.Property(() => t.Name).As("Name"),
Projections.Property(() => t.CreatedBy).As("CreatedBy"),
Projections.Property(() => t.CreatedDate).As("CreatedDate"),
Projections.Property(() => t.LastModifiedBy).As("LastModifiedBy"),
Projections.Property(() => t.LastModifiedDate).As("LastModifiedDate"),
Projections.Count(() => t.Articles).As("ArticlesCount"))
.TransformUsing(Transformers.AliasToBean<TagView>())
.List<TagView>();
這是我第一次使用投影,我不知道如何使它起作用。 我在這里想念什么嗎?
您收到的錯誤表明NHibernate為投影生成的SQL無法以其當前形式執行。 查詢正在
SELECT this_.intid AS y0_,
this_.vcname AS y1_,
this_.vccreatedby AS y2_,
this_.dtcreated AS y3_,
this_.vclastmod AS y4_,
this_.dtlastmod AS y5_,
Count(this_.intid) AS y6_
FROM tb_tag this_
WHERE this_.btactive = @p0
這完全是由於count是一個聚合需要一個聚合函數這一事實。
您可以將“文章”集合標記為“懶惰”和“多余”,並在不加載整個集合的情況下獲取“ Article.Count”值,如果這就是為什么要為其投影每列的原因。 有很多有效的文章可以指導您將某個收藏標記為特別懶惰 。
在閱讀了NHIibernate API和此鏈接的答案后,終於找到了一種使之工作的方法。
這是我的最終代碼:
Tag t= null;
TagView tv = null;
tags = _session.QueryOver(() => t)
.Left.JoinQueryOver(() => t.Articles, () => a)
.SelectList(list => list
.SelectGroup(() => t.Id).WithAlias(() => tv.Id)
.SelectGroup(() => t.Name).WithAlias(() => tv.Name)
.SelectGroup(() => t.CreatedBy).WithAlias(() => tv.CreatedBy)
.SelectGroup(() => t.CreatedDate).WithAlias(() => tv.CreatedDate)
.SelectGroup(() => t.LastModifiedBy).WithAlias(() => tv.LastModifiedBy)
.SelectGroup(() => t.LastModifiedDate).WithAlias(() => tv.LastModifiedDate)
.SelectCount(() => t.Articles).WithAlias(() => tv.ArticlesCount))
.TransformUsing(Transformers.AliasToBean<TagView>())
.List<TagView>();
我需要使用.SelectList而不是.Select來投影列表,還要使nhibernate成功計數需要將其與相關表一起加入的文章,以在將類初始化標記文章映射為ExtraLazyLoad屬性時也加快查詢速度。 希望我也能幫助遇到同樣問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.