hql 加入@CollectionTable

2024-03-18

我有一个域名Service有收藏tags如下 :

@Entity
public class Service extends AbstractEntity<Long> {
            private static final long serialVersionUID = 9116959642944725990L;

        @ElementCollection(fetch = FetchType.EAGER, targetClass = java.lang.String.class)
        @CollectionTable(name = "service_tags", joinColumns = @JoinColumn(name = "s_id"))
        @Column(name = "tag")
        private Set<String> tags;
    }

我想选择Services with特别的KEY of Service.tags.

hql加盟Service to Service.tags如下:

select s from Service s INNER JOIN s.tags t where s.status=0 and (s.serviceType=9 or t.tag in ('College'))

但是,上面的 hql 返回给我,但有以下异常:

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: cannot dereference scalar collection element: tag [select s from com.zazzercode.domain.Service s INNER JOIN s.tags t where s.status=0 and (s.serviceType=9 or t.tag in ('College')) ]

select s from Service s INNER JOIN s.tags t where s.status=0虽然有效。

看着JPQL 查询非实体集合 https://stackoverflow.com/a/9311242/432903,我尝试如下

"select s from Service s where s.status=0 and s.priviligedUser.priviligedUserType IN (2,4) and (s.serviceType=9 or (KEY(s.tags)='tag' and (VALUE(s.tags)='College'))"

出现以下异常:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: null near line 1, column 188 [select s from com.esewa.server.entity.Service s where s.status=0 and (s.serviceType=9 or (KEY(s.tags)='tag' and (VALUE(s.tags)='College'))]

我已经取得了同样的成就使用标准 API https://stackoverflow.com/a/14795429/432903几个月前。


谢谢JPQL 查询非实体集合 https://stackoverflow.com/a/9311242/432903

以下代码有效!

"select s from Service s INNER JOIN s.tags t where s.status=0 and and (s.serviceType=9 or  VALUE(s.tags) in ('College')) "
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hql 加入@CollectionTable 的相关文章

随机推荐