1

我正在使用连接在 HQL 中构建查询,并且我已经堆叠了一件事:

我的查询看起来像:

From HistoryPerPhraseEntity as history left join history.linkAddressByLinkId where history.serviceId = :serviceId

并且查询很好,但是当我试图将响应放入我的数据库对象并尝试获取如下值时:

historyPhrase.get(0).getPhraseId()

我有:

[Ljava.lang.Object; cannot be cast to com.rasp.lta.domain.HistoryPerPhraseEntity

但改成这样后:

Select history From HistoryPerPhraseEntity as history left join history.linkAddressByLinkId where history.serviceId = :serviceId

一切都好。

有人可以回答为什么我在此查询中需要“选择”吗?

谢谢

4

1 回答 1

2

因为没有明确的 SELECT Hibernate 无法确定只应返回实例 HistoryPerPhraseEntity。它还返回加入的实体。

这就是它返回对象数组列表的原因。数组的第一个索引包含 HistoryPerPhraseEntity,第二个索引中的对象类型与 linkAddressByLinkId 集合中的元素类型相同。

如果您出于某种原因只想访问第一个查询返回的 HistoryPerPhraseEntity,可以通过将返回列表的每个元素转换为Object[]并转换Object[0]HistoryPerPhraseEntity

不过最好使用 SELECT 。

于 2012-07-02T17:14:41.143 回答