标准+;nhibernate+;基于2 id和xB4检索对象;s

标准+;nhibernate+;基于2 id和xB4检索对象;s,nhibernate,criteria,Nhibernate,Criteria,我想根据2个id检索一个对象 这是正确的标准吗 var notes = session .CreateCriteria(typeof(Note)) .Add(Restrictions.Eq("Product", productId)) .Add(Restrictions.Eq("User", userId)) .List<Note>(); var note

我想根据2个id检索一个对象

这是正确的标准吗

var notes = session
                .CreateCriteria(typeof(Note))
                .Add(Restrictions.Eq("Product", productId))
                .Add(Restrictions.Eq("User", userId))
                .List<Note>();
var notes=会话
.CreateCriteria(类型(注))
.Add(Restrictions.Eq(“产品”,productId))
.Add(Restrictions.Eq(“用户”,userId))
.List();
此查询是否获取属于用户和产品的注释

这是否符合以下标准:

var comments = session
                .CreateCriteria(typeof(Comment))
                .Add(Restrictions.Eq("Product", productId))
                .Add(Restrictions.Eq("IsOwnComment", 0))
                .List<Comment>();
            return comments;
var注释=会话
.CreateCriteria(类型(注释))
.Add(Restrictions.Eq(“产品”,productId))
.Add(Restrictions.Eq(“IsOwnComment”,0))
.List();
返回评论;
返回布尔值为0的产品的注释

我做对了吗

br

var notes=session
.CreateCriteria(类型(注))
.Add(Restrictions.Eq(“产品”,productId))
.Add(Restrictions.Eq(“用户”,userId))
.List();
这样不行。您需要提供产品和用户关联路径的整个实体。您可以使用特殊id属性:

var notes = session
                .CreateCriteria(typeof(Note))
                .Add(Restrictions.Eq("Product.id", productId))
                .Add(Restrictions.Eq("User.id", userId))
                .List<Note>();
var notes=会话
.CreateCriteria(类型(注))
.Add(Restrictions.Eq(“Product.id”,productId))
.Add(Restrictions.Eq(“User.id”,userId))
.List();

第二个示例,当您使用添加条件表达式而不标识它们之间的And或or表达式时,请使用
限制.IsNull(“IsOwnComment”)
,默认情况下,NHibernate将使用和,以便您编写的准则与这些HQL查询相同:

"from Note note where note.Product=:productId and note.User=:userId"


只有一件事需要注意:如果产品和用户是多对一关系,那么这些查询将不起作用,您应该使用Product.Id和User.Id作为属性名

如何在hql和条件之间进行转换?是的,它们是多对一的。我如何根据标准识别它们,比如和或?或者?你能给我举个例子让我理解,这样我就可以制定更多的标准。像这样。Add(Restrictions.Eq(“Product”,productId)和&Restrictions.Eq(“User”,userId))Add.Restrictions.Eq(“Product.Id”,productId)要创建复杂的条件,您可以使用AndExpression和OrExpression(二进制表达式),将两个表达式合并起来。这可能会有所帮助。它有一些例子
"from Note note where note.Product=:productId and note.User=:userId"
"from Comment comment where comment.Product=:productId and comment.IsOwnComment=0"