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 Service
s 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.
Thanks to 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')) "
"select s from Service s where s.status=0 and (s.serviceType=9 or 'College' in elements(s.tags))"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With