可以说我有一个查询,该查询获取具有以下Set标签​​的List Item Objects:

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');


总部:

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。

10-07 13:14