Hibernate JPQL - 在地图关联错误中查询KEY() [英] Hibernate JPQL - querying for KEY() in Map association error

查看:114
本文介绍了Hibernate JPQL - 在地图关联错误中查询KEY()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个JPQL查询,该查询应该从一个地图关联中获取一个实体和关键字,并且出现bizzare错误。



我的设置是JPA2使用Hibernate(3.5)实现。



模型如下:

I有一个Department实体bean,例如:

  @Entity 

public class Department {

  @Id 
@SequenceGenerator name =DEPARTMENT_ID_GENERATOR,sequenceName =department_sequence,allocationSize = 100)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =DEPARTMENT_ID_GENERATOR)
@Column(unique = true,nullable = false)
保护长ID;

@OneToMany(fetch = FetchType.EAGER)
私人地图< String,Phone> phones = new HashMap< String,Phone>();

// ... getters和setters关注



以及它的关联实体:

  @Entity 

公共类电话{

  @Id 
@SequenceGenerator
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =PHONE_ID_GENERATOR)
@Column(unique = true,nullable = false)$ name =PHONE_ID_GENERATOR,sequenceName =phone_sequence
保护长ID;
私人诠释数字;

// ... getters和setters关注



现在我想,按照掌握JPA2 ...的书,我可以做一个JPQL,例如:

  String queryString2 =SELECT d,KEY(p)FROM Department d JOIN d.phones p WHERE p ='internal'; 

但是这一切都让我感到一阵惊愕:

  java.lang.IllegalStateException:节点没有数据类型:org.hibernate.hql.ast.tree.MethodNode 

$ b

- [METHOD_CALL] MethodNode:'('
+ - [METHOD_NAME] IdentNode:'KEY'{originalText = KEY}
- [EXPR_LIST] SqlNode:'exprList'
- [ALIAS_REF] IdentNode:'phone2_.id'{alias = p,className = model.Phone,tableAlias = phone2 _}

  at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:156)... 

有人可以告诉我,我使用的JPQ是否是错误的,如果是这样的话,那么获取实体和仅从其地图上的密钥您的语法是正确的,这是因为地图的KEY& VALUE没有实现:

解决方案

a href =https://hibernate.onjira.co m / browse / HHH-5396rel =nofollow> https://hibernate.onjira.com/browse/HHH-5396 我不知道其他查询。


I'm trying to make a JPQL query that should fetch an entity and the keys from one of its map associations, and I'm getting a bizzare error.

My setup is JPA2 using the Hibernate (3.5) implementation.

The model is as follows:

I have a Department entity bean such as:

@Entity

public class Department {

@Id
@SequenceGenerator(name = "DEPARTMENT_ID_GENERATOR", sequenceName="department_sequence", allocationSize=100)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "DEPARTMENT_ID_GENERATOR")
@Column(unique = true, nullable = false)
protected Long id;

@OneToMany(fetch=FetchType.EAGER)
private Map<String,Phone> phones = new HashMap<String, Phone>();

//... getters and setters follow

and it's associated entity :

@Entity

public class Phone {

@Id
@SequenceGenerator(name = "PHONE_ID_GENERATOR", sequenceName="phone_sequence", allocationSize=100)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "PHONE_ID_GENERATOR")
@Column(unique = true, nullable = false)
protected Long id;
private int number;

//... getters and setters follow

Now I thought, as per the "Mastering JPA2..." book, that I could do a JPQL such as:

String queryString2 = "SELECT d, KEY(p) FROM Department d JOIN d.phones p WHERE p='internal'";

but all this gets me a bizzare error:

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode 

-[METHOD_CALL] MethodNode: '(' +-[METHOD_NAME] IdentNode: 'KEY' {originalText=KEY} -[EXPR_LIST] SqlNode: 'exprList' -[ALIAS_REF] IdentNode: 'phone2_.id' {alias=p, className=model.Phone, tableAlias=phone2_}

at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:156)...

Can someone please tell me if the JPQ I'm using is wrong, and if so, what might be a correct alternative to getting an entity and ONLY the keys from one of its map associations?

解决方案

Your syntax is correct, this is because KEY & VALUE for maps are not implemented: https://hibernate.onjira.com/browse/HHH-5396 I am not aware of alternative query.

这篇关于Hibernate JPQL - 在地图关联错误中查询KEY()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆