查询集合类型的属性包含某值
用 HQL 的方式,这种方式好处是可以添加 distinct
@Transactional(readOnly = true)
@CompileDynamic
Collection<Vlog> listVlogOfAllSubSpecials(int offset = 0, int max = 10) {
Vlog.findAll("""
select distinct v
from Vlog as v
join v.tags t
where t.id in (select id from VlogTag as vt where vt.system=true)
""", [offset: offset, max: max])
}
用 Criteria 的方式,这种方式去重工作是拿到结果集合后在内存中完成的,而不是在数据库中。
List<VlogTag> allSubSpecials = VlogTag.findAllBySystem(true)
def c = Vlog.createCriteria()
c.listDistinct {
tags {
inList "id", allSubSpecials*.id
}
order "dateCreated", "desc"
firstResult(offset)
maxResults(max)
} as Collection