【问题标题】:Eager loading with HQL使用 HQL 急切加载
【发布时间】:2010-03-28 19:58:47
【问题描述】:

是否可以在不触及映射的情况下使用 HQL 进行预加载? nHibernate 完全忽略了“left join fetch”表达式。

var query = Session.CreateQuery("from o in Member left join fetch o.Photos");
query.List<Member>();

生成的SQL是

SELECT this_.Id as Id7_1_, this_.Name as Name7_1_, this_.AltNames as AltNames7_1_,
this_.Tags as Tags7_1_, this_.Loved as Loved7_1_, profile2_.id as id6_0_,
profile2_.Website as Website6_0_, profile2_.Email as Email6_0_, 
profile2_.Shop as Shop6_0_ 
FROM Member this_ 
left outer join member_profile profile2_ on this_.Id=profile2_.id 
limit 10;

然后是 10 条抓取照片的声明。 MemberProfile 映射为 OneToOne。

【问题讨论】:

    标签: c# nhibernate


    【解决方案1】:

    您可以使用fetch 关键字:

    from Cat as cat inner join fetch cat.Mate
    

    这将急切地加载Mate 关联。

    【讨论】:

    • 它只是完全忽略了“join fetch”部分。
    • 左连接怎么样:from Cat as cat left join cat.Kittens
    • 我已经更新了问题。任何类型的连接提取都会被忽略。
    • 你的HQL语法很奇怪。试试这个:from Member as m left join fetch m.Photos
    • 我已经弄清楚了,我的代码中还有其他一些错误。仍然感谢您的回答,很高兴知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2013-07-29
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多