Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hql join @CollectionTable

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.

like image 546
prayagupa Avatar asked Sep 06 '13 12:09

prayagupa


2 Answers

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')) "
like image 114
prayagupa Avatar answered Nov 14 '22 06:11

prayagupa


"select s from Service s where s.status=0 and (s.serviceType=9 or 'College' in elements(s.tags))"
like image 44
Skeeve Avatar answered Nov 14 '22 07:11

Skeeve