SetFetchMode 调用被忽略
我在以下查询中的 Criteria API 中调用 SetFetchMode 时遇到问题:
DetachedCriteria.For<User>()
.Add<User>(u => u.Status == UserStatus.Live)
.CreateAlias("UniqueId", "uid")
.CreateAlias("Companies", "comp")
.Add(Restrictions.Disjunction()
.Add(Restrictions.Like("uid.Uid", context.Text, MatchMode.Anywhere))
.Add(Restrictions.Like("comp.Name", context.Text, MatchMode.Anywhere)))
.SetFetchMode("Companies", FetchMode.Eager));
我的类:
public class User : EntityBase<int>
{
public virtual UniqueId UniqueId { get; set; }
public virtual ISet<Company> Companies { get; set; }
}
public class Company : EntityBase<int>
{
public virtual string Name { get; set; }
}
public class UniqueId : EntityBase<int>
{
public virtual string Uid { get; set; }
}
和映射
public sealed class UserMap : ClassMap<User>
{
public UserMap()
{
Table("users");
Id(x => x.Id).GeneratedBy.Native().Column("id");
References(x => x.UniqueId).Column("int_unique_id_ref");
HasMany(x => x.Companies)
.KeyColumn("user_id")
.Inverse()
.AsSet();
}
}
public sealed class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
Table("company");
Id(x => x.Id).GeneratedBy.Native().Column("id");
Map(x => x.Name).Column("name");
}
}
public sealed class UniqueIdMap : ClassMap<UniqueId>
{
public UniqueIdMap()
{
Table("tbl_trading_partner_unique_id");
Id(x => x.Id).GeneratedBy.Native().Column("int_id");
Map(x => x.Uid).Column("str_unique_id");
}
}
但是在获取用户列表后,Nhibernate 再次查询数据库以再次获取每个用户的公司集合。 NHibernate 只是忽略 SetFetchMode 的调用,因为我尝试编写如下内容:
.SetFetchMode("NotExistingProp", FetchMode.Eager)
Nhibernate 不会抛出任何异常。
我还尝试在映射中将 Lazy 设置为 false,但它也没有帮助。 不知道如何修复它,有人可以
在 Nhibernate 加载实体集合后进行修复吗?但他仍然忽略SetFetchMode,我可以在那里写任何东西。
I have a problem with SetFetchMode call in Criteria API in following query:
DetachedCriteria.For<User>()
.Add<User>(u => u.Status == UserStatus.Live)
.CreateAlias("UniqueId", "uid")
.CreateAlias("Companies", "comp")
.Add(Restrictions.Disjunction()
.Add(Restrictions.Like("uid.Uid", context.Text, MatchMode.Anywhere))
.Add(Restrictions.Like("comp.Name", context.Text, MatchMode.Anywhere)))
.SetFetchMode("Companies", FetchMode.Eager));
My Classes:
public class User : EntityBase<int>
{
public virtual UniqueId UniqueId { get; set; }
public virtual ISet<Company> Companies { get; set; }
}
public class Company : EntityBase<int>
{
public virtual string Name { get; set; }
}
public class UniqueId : EntityBase<int>
{
public virtual string Uid { get; set; }
}
And mappings
public sealed class UserMap : ClassMap<User>
{
public UserMap()
{
Table("users");
Id(x => x.Id).GeneratedBy.Native().Column("id");
References(x => x.UniqueId).Column("int_unique_id_ref");
HasMany(x => x.Companies)
.KeyColumn("user_id")
.Inverse()
.AsSet();
}
}
public sealed class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
Table("company");
Id(x => x.Id).GeneratedBy.Native().Column("id");
Map(x => x.Name).Column("name");
}
}
public sealed class UniqueIdMap : ClassMap<UniqueId>
{
public UniqueIdMap()
{
Table("tbl_trading_partner_unique_id");
Id(x => x.Id).GeneratedBy.Native().Column("int_id");
Map(x => x.Uid).Column("str_unique_id");
}
}
But after getting users list Nhibernate is quering data base again to get companies collection for each user once again. NHibernate just ignores call of SetFetchMode, because I have tried to write something like this:
.SetFetchMode("NotExistingProp", FetchMode.Eager)
Nhibernate doesn't thows any exceptions.
I also have tried to set Lazy to false in the mappings, but it also didn't help.
Have no idea how to fix it, can somebody
and after that Nhibernate loaded collection with entities. But he is still ignoring SetFetchMode, I can write anything there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案并不是那么明显。我们已更改
为
The solution is not so obvious. We have changed
to
由于您正在使用
.CreateAlias("Companies", "comp")
,NHibernate 无法预先加载该集合,因为您可能对该别名使用了限制。我的总体建议是永远不要在集合上使用急切加载。使用
batch_size
或 这个技巧。Since you are using
.CreateAlias("Companies", "comp")
, NHibernate can't eager-load the collection, as you could be using restrictions on that alias.My overall suggestion is to never use eager load on collections. Use
batch_size
or this trick instead.