hql join @CollectionTable

北城余情 提交于 2020-01-03 20:04:35

问题


I have a domain Service with collection tags as below :

@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;
    }

I want to select Services with particular KEY of Service.tags.

hql joining Service to Service.tags is as below :

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

But, above hql returns me with following exception :

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 works though.

Looking at JPQL querying a collection of non-entites, I tried like below

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

Got following exception :

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

I have achieved same thing using criteria api some months back.


回答1:


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



回答2:


Thanks to JPQL querying a collection of non-entites

Following code works !!!

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


来源:https://stackoverflow.com/questions/18657422/hql-join-collectiontable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!