hql join @CollectionTable

时间:2013-09-06 12:03:45

标签: hibernate hql jpql

我有一个带有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;
    }

我想选择Service with KEY Service.tags hql

Service加入Service.tagsselect s from Service s INNER JOIN s.tags t where s.status=0 and (s.serviceType=9 or t.tag in ('College')) 如下:

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

但是,上面的hql会返回以下异常:

select s from Service s INNER JOIN s.tags t where s.status=0

"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'))" 虽然有效。

查看JPQL querying a collection of non-entites,我尝试过以下

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

遇到以下异常:

{{1}}

几个月前我已经完成了相同的事情using criteria api

2 个答案:

答案 0 :(得分:3)

"select s from Service s where s.status=0 and (s.serviceType=9 or 'College' in elements(s.tags))"

答案 1 :(得分:1)

感谢JPQL querying a collection of non-entites

以下代码有效!!!

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