HQL查询,从多个Item对象实例中检索通用标记



假设我有一个查询,它得到了以下带有如下标签集的列表项对象:

Item1{TagSet{tag1,tag2,tag3}}
Item2{TagSet{tag3,tag4,tag5}}
Item3{TagSet{tag6,tag7,tag8}}
Item1 Item2 Item3 are instances of Item object in List 
TagSet is Set Collection object

哪个HQL或SQL查询将检索List of Items having tag3,并从该结果中检索get list of all tags from thouse items that contain tag3not including tag3),例如从上面的3个项目中检索?

期望的结果是:

TagSet{tag1, tag2, tag4, tag5}

您没有给我们您的模式,所以我猜在这里,但是。。。

SQL:

select distinct *
from item
join tag on tag.item_id = item.id
where item.name = 'foo'
and tag.name != 'tag3'
and item.id in (select item_id from tag where name = 'tag3');

HQL:

entityManager
.createQuery("
    select t
    from Tag t
    where t.name != 'tag3'
    and t.item in (select t2.item from Tag t2 where t2.name = 'Tag3')
    and t.item.name = :name")
.setParateter("name", "foo")
.getResultList();

然后使用tag.getItem()获取返回的标记的Items。

最新更新