[英]Applying AND condition on one field grails domain
In Grails, I have two domain classes having many to many relationship 在Grails中,我有两个具有多对多关系的领域类
class VisitingUser
{
String name
static hasMany = [visitedCountries:VisitedCountry]
static belongsTo = VisitedCountry
}
class VisitedCountry
{
String countryName
static hasmany=[users:VisitingUser]
}
I want all those users who visited country "india" AND "usa" both. 我想要所有访问过“印度”和“美国”两个国家的用户。
Currently I am solving this problem by getting two list of visitingUsers, one for "india" and other for "usa" and then putting them in a Set. 目前,我正在解决此问题,方法是获取两个visitorUsers列表,一个用于“印度”,另一个用于“美国”,然后将它们放在集合中。
I want a solution with using createCriteria or dynamic finder. 我想要使用createCriteria或动态查找器的解决方案。
I don't think this is possible with a criteria query, and it's definitely not possible with a dynamic finder. 我认为使用条件查询是不可能的,而使用动态查找程序绝对不可能。 Here's a solution with HQL though:
这是HQL的解决方案:
VisitingUser.executeQuery('''
select distinct u from VisitingUser u where
u in (
select u from VisitingUser u join u.visitedCountries country where country.countryName = 'usa'
)
and u in (
select u from VisitingUser u join u.visitedCountries country where country.countryName = 'india'
)
''')
Two unrelated things - VisitedCountries
should be called VisitedCountry
(although the set name visitedCountries
makes sense because it will potentially have multiple elements), and the relationship should be many-to-many. 两个不相关的事物-
VisitedCountries
应该叫VisitedCountry
(虽然集名称visitedCountries
是有道理的,因为这将有可能有多个元素),和的关系应该是多到很多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.