JPQL查询非实体的集合

时间:2012-02-16 12:08:02

标签: java jpa jpql

我想用一组非实体进行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和hibernate 3.3。 JBoss 5中的默认库

1 个答案:

答案 0 :(得分:6)

JPA 2 spec(第139页)定义了访问密钥的KEY()VALUE()函数以及映射值元素集合的值:

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