我有一个标签类,它与文章类有多对多的关系,问题是我想在标签视图模型类中表示一个投影所以列结果应该是这样的
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
这是我的标签类:
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是一个聚合需要一个聚合函数。
您可以将文章集合标记为lazy和extra并获取文章。计数值,而不加载整个集合,如果这就是为什么要投影每个列的原因。有相当多有效的文章可以指导您将集合标记为extra lazy。
在阅读了hibernate 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属性。希望我也能帮助那些面临同样问题的人。