【问题标题】:JPQL querying a collection of non-entitesJPQL 查询非实体集合
【发布时间】:2012-02-16 12:08:01
【问题描述】:

我想用一组非实体进行 JPQL 查询。这是我的表实体:

@Entity
@Table(name = "ct_table")
public class Table {
...

@CollectionOfElements(fetch = FetchType.EAGER)
@JoinTable(name = "ct_table_result", joinColumns = @JoinColumn(name = "tableId"))
@MapKey(columns = { @Column(name = "label") })
@Column(name = "value")
private Map<String, String> tableResults;
...

然后我尝试进行这样的查询

select count(*) from table where table.tableResults['somekey'].value='somevalue'

但我得到以下异常:

Cannot create element join for a collection of non-entities!

有什么建议吗??

感谢您的宝贵时间

编辑:

我使用 JPA 1 和休眠 3.3。 JBoss 5 中的默认库

【问题讨论】:

    标签: java jpa jpql


    【解决方案1】:

    JPA 2 spec(第 139 页)定义了 KEY()VALUE() 函数来访问映射值元素集合的键和值:

    select count(t.id) from Table t 
    where KEY(t.tableResults) = 'somekey' and VALUE(t.tableResults) = 'somevalue'
    

    【讨论】:

    • 在 JPA 1 中?我正在使用 Jboss 5.5,我必须使用 JPA 1 我不能使用 JPA 2
    • 您使用的 Hibernate 版本是什么?
    • JPA 1 和休眠 3.3。 Jboss 5 中的默认库
    • 不幸的是,尽管这个版本的 Hibernate 支持 JPA2、AFAIK,但仍然没有实现对 KEY 和 VALUE 的支持(参见hibernate.onjira.com/browse/HHH-5396),即使在当前版本中也是如此。 Hibernate 作为 index() 函数,但我不确定您是否可以在元素集合上使用它。见docs.jboss.org/hibernate/core/3.3/reference/en-US/html_single。请尝试以下操作:从 Table t 中选择 count(t.id) join t.tableResults r where index(r) = 'somekey' and r = 'somevalue'。您也可以使用实体映射,而不是元素映射。
    猜你喜欢
    • 2014-05-04
    • 2018-06-07
    • 2012-10-31
    • 2014-10-26
    • 1970-01-01
    • 2014-02-21
    • 2013-04-23
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多