【问题标题】:hql join @CollectionTablehql 加入@CollectionTable
【发布时间】:2013-09-10 12:47:40
【问题描述】:

我有一个域 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 中的Service.tags

hql 加入ServiceService.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 querying a collection of non-entites,我尝试如下

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

几个月前我在using criteria api 也取得了同样的成就。

【问题讨论】:

    标签: hibernate hql jpql


    【解决方案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')) "
    

    【讨论】:

    • 这个接受的答案对我不起作用,因为 VALUE 仅适用于地图,但这是一个集合。
    【解决方案2】:
    "select s from Service s where s.status=0 and (s.serviceType=9 or 'College' in elements(s.tags))"
    

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 2021-01-27
      相关资源
      最近更新 更多