NHibernate Criteria 子查询帮助
我正在使用 NHibernate,我的要求是我有 2 个表,用户和票证。我想要用户中但不在票证中的所有记录。 Ticket 表将 UserId 作为 User 表主键 ID 的参考键。下面是我的代码,
RegNotTickTemplate.Criteria = DetachedCriteria.For(typeof(User));
RegNotTickTemplate.Criteria.Add(Subqueries.PropertyNotIn("ID",DetachedCriteria.For(typeof(Ticket))
.SetProjection(Projections.Property("UserID"))));
上面的查询没有返回正确的记录集。
I am using NHibernate and I my requirement is that I have 2 tables, User and Ticket. I want all the records that are in User but not in Ticket. The Ticket table has UserId as reference key to the Primary key ID of User table. Below is my code,
RegNotTickTemplate.Criteria = DetachedCriteria.For(typeof(User));
RegNotTickTemplate.Criteria.Add(Subqueries.PropertyNotIn("ID",DetachedCriteria.For(typeof(Ticket))
.SetProjection(Projections.Property("UserID"))));
The above query doesnt return correct set of records.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为替代方案,您可以尝试使用 HQL。我从不使用 Criteria,因为我发现 HQL 更具可读性(它几乎与 SQL 相同,只是你可以根据实体而不是表进行查询)。
第 13 章 HQL:Hibernate 查询语言
如果仍然不行,您随时可以执行 SQL 查询
As an alternative, you could try using HQL. I never use Criteria as I find HQL more readable (it is almost the same as SQL except you get to query based on the entities rather than the tables).
Chapter 13. HQL: The Hibernate Query Language
If that is still no good, you can always do an SQL query
您缺少的是:该用户的票数应该大于零。以下是实现它的方法:
如果您不需要行数,可以执行以下操作:
What you are missing is: the count of tickets for this user should be greater than zero. Here is how you can implement it:
If you don't want the count of rows you can do the following: