Reputation: 67
I want to retrieve a object based on 2 id´s.
Is this the right critera for this?
var notes = session
.CreateCriteria(typeof(Note))
.Add(Restrictions.Eq("Product", productId))
.Add(Restrictions.Eq("User", userId))
.List<Note>();
does this query fetch the notes that belongs to the user and the product?
and does this criteria:
var comments = session
.CreateCriteria(typeof(Comment))
.Add(Restrictions.Eq("Product", productId))
.Add(Restrictions.Eq("IsOwnComment", 0))
.List<Comment>();
return comments;
return the comments of the products where the boolean value is 0?
have i done this right?
br
Upvotes: 1
Views: 166
Reputation: 5320
When you use Add expression of Criteria without identifying And or Or expressions in between them NHibernate will use And by default so the critera that you've written will be identical to these HQL queries :
"from Note note where note.Product=:productId and note.User=:userId"
and
"from Comment comment where comment.Product=:productId and comment.IsOwnComment=0"
there's just one thing that should be noticed:if Product and User are many to one relationships these queries won't work instead you should use Product.Id and User.Id as the property name
Upvotes: 1
Reputation: 8201
var notes = session
.CreateCriteria(typeof(Note))
.Add(Restrictions.Eq("Product", productId))
.Add(Restrictions.Eq("User", userId))
.List<Note>();
This will not work like this. You would need to provide entire entity of association path of Product and User. What you can do is use the special id property:
var notes = session
.CreateCriteria(typeof(Note))
.Add(Restrictions.Eq("Product.id", productId))
.Add(Restrictions.Eq("User.id", userId))
.List<Note>();
For second example, use Restrictions.IsNull("IsOwnComment")
Upvotes: 2