Nhibernate queryover和transformusing失去了延迟加载的能力
我想尝试将Nhibernate queryover和transformusing失去了延迟加载的能力,nhibernate,distinct,queryover,Nhibernate,Distinct,Queryover,我想尝试将DISTINCT关键字引入SQL,基本上我需要以下SQL:- SELECT distinct this_.Id as y0_, this_.Name as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Director as y4_ FROM
DISTINCT
关键字引入SQL,基本上我需要以下SQL:-
SELECT distinct this_.Id as y0_,
this_.Name as y1_,
this_.Description as y2_,
this_.UnitPrice as y3_,
this_.Director as y4_
FROM Product this_
inner join ActorRole actor1_
on this_.Id = actor1_.MovieId
WHERE this_.ProductType = 'Movie'
AND actor1_.Name like 'm%' /* @p0 */
QueryOver代码如下所示,但是如果不使用投影,我无法使用DISTINCT关键字:-
var movie = Session.QueryOver<Movie>()
.JoinQueryOver<Actor>(m => m.ActorList).Where(a => a.Name.IsLike("m%"))
.Select(
Projections.Distinct(
Projections.ProjectionList()
.Add(Projections.Property<Movie>(w => w.Id))
.Add(Projections.Property<Movie>(w => w.Name))
.Add(Projections.Property<Movie>(w => w.Description))
.Add(Projections.Property<Movie>(w => w.UnitPrice))
.Add(Projections.Property<Movie>(w => w.Director))
)
)
.TransformUsing(Transformers.AliasToBean<Movie>());
return movie.List<Movie>();
var movie=Session.QueryOver()
.JoinQueryOver(m=>m.ActorList)。其中(a=>a.Name.IsLike(“m%”)
.选择(
投影,清晰(
投影。投影列表()
.Add(projects.Property(w=>w.Id))
.Add(Projections.Property(w=>w.Name))
.Add(projects.Property(w=>w.Description))
.Add(projects.Property(w=>w.UnitPrice))
.Add(projects.Property(w=>w.Director))
)
)
.TransformUsing(Transformers.AliasToBean());
返回movie.List();
这部作品让我想起了演员以字母“m”开头的不同电影。现在问题来了,因为投影是针对DTO的,当我迭代结果并希望延迟加载子对象时。例如:
@foreach (var item in Model.ActorList)
{
<li>@(item.Name) <em>plays</em> @item.Role</li>
}
@foreach(Model.ActorList中的变量项)
{
@(item.Name)扮演@item.Role
}
Model.ActorList
始终为NULL
,由于此方法是为DTO设计的,因此投影和使用转换器似乎会丢失延迟加载。我有什么选择
我知道我可以使用子查询或HQL而不是
选择distinct
Transformers.AliasToBean()
只创建一个新电影并填充属性。因此,它是一部新电影,不是从DB加载的,因此不会继承原始电影的集合。AFAIKAliasToBean
是用投影数据填充视图模型等
你就不能用:
Session.QueryOver<Movie>()
.JoinQueryOver<Actor>(m => m.ActorList).Where(a => a.Name.IsLike("m%"))
.List();
Session.QueryOver()
.JoinQueryOver(m=>m.ActorList)。其中(a=>a.Name.IsLike(“m%”)
.List();
如果其他人对此感兴趣,请阅读解释此行为的说明谢谢您的回答,但如果两个演员的名字以字母m%
开头,则会返回重复的电影。我知道我可以使用.TransformUsing(CriteriaSpecification.DistincTrotenty)
,但这会在客户端进行过滤。我特别想知道我是否可以使用QueryOver goodness在这里使用DISTINCT
关键字!同样,我可以回退到HQL来获得我想要的东西,但我正在尝试更多地学习QueryOver,并找出它的困难所在。另一种选择是使用qanted电影的Id进行子查询,然后使用。子查询(x=>x.Id)。在(子查询)
是的,我现在这样做,然而,最佳的sql可能是选择不同的电影
,并使用内部连接
+1获取想法:)你真的应该将博客文章的相关内容发布到这个答案上