Hibernate JPQL - 在地图关联错误中查询 KEY()
我正在尝试创建一个 JPQL 查询,该查询应该从其映射关联之一获取实体和键,但我收到了一个奇怪的错误。
我的设置是使用 Hibernate (3.5) 实现的 JPA2。
模型如下:
我有一个 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)
protected Long id;
@OneToMany(fetch=FetchType.EAGER)
private Map<String,Phone> phones = new HashMap<String, Phone>();
//... getters 和 setters follow
及其关联实体:
@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 和 setters follow
现在我想,根据“Mastering JPA2...”一书,我可以执行 JPQL,例如:
String queryString2 = "SELECT d, KEY(p) FROM Department d JOIN d.phones p WHERE p='internal'";
但这一切都会给我带来一个奇怪的错误:
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)...
有人可以告诉我我使用的 JPQ 是否错误,如果是,正确的可能是什么除了获取实体并且仅获取其地图关联之一的键之外,还有其他选择吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于下一位读者,Mikko Maunu 提到的错误昨天刚刚解决,所以我们只需要等待下一个版本:)
For next readers, the bug mentionned by Mikko Maunu has just been resolved yesterday, so we just need to wait the next release :)
你的语法是正确的,这是因为 KEY &地图的 VALUE 未实现:
https://hibernate.onjira.com/browse/HHH-5396 我不知道的替代查询。
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.