Hibernate JPQL - 在地图关联错误中查询 KEY()

发布于 2024-12-05 08:25:57 字数 1822 浏览 1 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春有你 2024-12-12 08:25:57

对于下一位读者,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 :)

可可 2024-12-12 08:25:57

你的语法是正确的,这是因为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文