I have entity Problem mapped using many-to-many to entity Tag (Problem has a list of tags). I want to load all the Problems, which have specific Tags.
For example:
Problem1 (tag1)
Problem2 (tag1, tag2)
Problem3 (tag1, tag3)
Problem4 (tag3, tag4)
I want to get the problems by filter "tag1, tag2". The system shall return:
Problem1 (tag1)
Problem2 (tag1, tag2)
Problem3 (tag1, tag3)
I have been puzzling over this problem for a week already. Have you any ideas?
P.S. Load all the list and filter it using LINQ is the last possible variant...
Asked
Active
Viewed 181 times
2
casperOne
- 72,334
- 18
- 180
- 242
Sergey Metlov
- 24,666
- 27
- 90
- 147
1 Answers
2
HQL is a better fit for these queries than Criteria.
One possible solution:
session.CreateQuery(@"select distinct p
from Problem p
join p.Tags t
where t in (:tags)")
.SetParameterList("tags", listOfTags)
Diego Mijelshon
- 52,160
- 15
- 117
- 151
-
Can I use HQL query and criteria together? The matter is I need paging too (SetFirstResult() and SetMaxResult()). – Sergey Metlov Sep 26 '10 at 15:09
-
HQL and Criteria are different methods. But you can use SetFirstResult and SetMaxResult on HQL queries too. – Diego Mijelshon Sep 26 '10 at 15:58