NHibernate HasMany 与 Criteria 中的 OrderBy 覆盖选项
var criteria = MakeCriteriaForFindAllStudents(exportCriteria)
.AddOrder<Entities.Student>(x => x.LastName, Order.Asc)
.AddOrder<Entities.Student>(x => x.FirstName, Order.Asc);
criteria.SetFetchMode("Addresses", FetchMode.Join)
.SetFetchMode("CR", FetchMode.Join)
.SetFetchMode("SEC", FetchMode.Join)
.SetFetchMode("Dep", FetchMode.Join)
.SetFetchMode("WR", FetchMode.Join)
.SetResultTransformer(new DistinctRootEntityResultTransformer());
var results = criteria
.GetExecutableCriteria(Session)
.Future<Entities.Student>();
AddOrder 似乎在某些条件下不起作用。
学生流利映射与 WR 具有以下关系。
HasMany(x => x.WR)
.Table("WR")
.KeyColumn("StudentId")
.LazyLoad()
.AsSet()
.Inverse()
.Cascade.None()
.OrderBy("CreatedDate desc")
.BatchSize(10);
因此,当调用 WR 上的 SetFetchMode 时,映射的 OrderBy 部分将取代代码中指定的 AddOrder。
sql 代码示例..
ORDER BY wr6_.CreatedDate desc, this_.LNametxt asc, this_.FNameTxt asc;
如何覆盖定义的映射并确保从 OrderBy 中删除 WR CreatedDate 并从 Criteria 中删除?
var criteria = MakeCriteriaForFindAllStudents(exportCriteria)
.AddOrder<Entities.Student>(x => x.LastName, Order.Asc)
.AddOrder<Entities.Student>(x => x.FirstName, Order.Asc);
criteria.SetFetchMode("Addresses", FetchMode.Join)
.SetFetchMode("CR", FetchMode.Join)
.SetFetchMode("SEC", FetchMode.Join)
.SetFetchMode("Dep", FetchMode.Join)
.SetFetchMode("WR", FetchMode.Join)
.SetResultTransformer(new DistinctRootEntityResultTransformer());
var results = criteria
.GetExecutableCriteria(Session)
.Future<Entities.Student>();
The AddOrder seems not to work in certain conditions.
The Student fluent mapping has the following relation to WR.
HasMany(x => x.WR)
.Table("WR")
.KeyColumn("StudentId")
.LazyLoad()
.AsSet()
.Inverse()
.Cascade.None()
.OrderBy("CreatedDate desc")
.BatchSize(10);
So when SetFetchMode on WR is called, the OrderBy part of the mapping supercedes the AddOrder specified in the code.
sample of the sql code..
ORDER BY wr6_.CreatedDate desc, this_.LNametxt asc, this_.FNameTxt asc;
How to override the mapping defined and ensure that WR CreatedDate is removed from OrderBy is removed from Criteria ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下生成的sql。也许该顺序包含在查询中的某个位置(即在子查询中),并且对结果集没有任何影响。取决于 MakeCriteriaForFindAllStudents() 如何构建 ICriteria。
take a look at the generated sql. Maybe the order is included somewhere in the query (i.e. in a subquery) and does just not have any effect on the result set. Depends on how MakeCriteriaForFindAllStudents() builds the ICriteria.
看起来这是 NHibernate 中的已知错误,这里是链接
http://www.primordialcode.com/blog/post/nhibernate-eager-fetch-order-strange-behavior-icriteria
Looks like it is know bug in NHibernate, here is the link
http://www.primordialcode.com/blog/post/nhibernate-eager-fetch-order-strange-behavior-icriteria