要遍历的节点不能为空(Hibernate SQL)

发布于 2024-11-03 20:10:21 字数 406 浏览 1 评论 0原文

我正在通过 hibernate 执行 SQL 查询,如下所示:

SELECT thi 
FROM track_history_items thi 
JOIN artists art 
  ON thi.artist_id = art.id 
WHERE thi.type = "TrackBroadcast" 
GROUP BY art.name 
ORDER thi.createdAt DESC

但我收到消息“要遍历的节点不能为空!”。有谁知道这可能是什么原因造成的?

--编辑--

我很确定这个问题是由 Artist_id 可能为空引起的。但是,我无法阻止这种情况,所以我可以跳过具有空 Artist_id 的 track_history_item 行吗?

I'm performing a SQL query through hibernate, which looks like:

SELECT thi 
FROM track_history_items thi 
JOIN artists art 
  ON thi.artist_id = art.id 
WHERE thi.type = "TrackBroadcast" 
GROUP BY art.name 
ORDER thi.createdAt DESC

but I'm getting the message that 'Node to traverse cannot be null!'. Anyone know what could be causing this?

--EDIT--

I'm pretty sure that this problem is being caused by the possibility of artist_id to be null. However, I can't prevent this, so can I just skip the rows track_history_item rows which have a null artist_id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

绝對不後悔。 2024-11-10 20:10:21

仅当使用 createQuery 而不是 createNamedQuery 时,我才收到该错误。
因此,当检测到这一点时,hibernate 会抛出异常。

Query query = entityManager.createQuery("DS.findUser");

I have gotten that error only when using createQuery instead of createNamedQuery.
So when detecting this, hibernate throws the exception.

Query query = entityManager.createQuery("DS.findUser");
三岁铭 2024-11-10 20:10:21

要遍历的节点不能为空!

这是一条通用的 Hibernate 错误消息,指示您的查询中存在语法问题。作为另一个示例,忘记以单词“SELECT”开始 select 子句会产生相同的错误。

在这种情况下,语法错误是由于 on 子句造成的 - HQL 不支持它们。相反,像这样进行交叉连接:

FROM track_history_items thi, artists art 
WHERE thi.type = "TrackBroadcast" 
AND  thi.artist_id = art.id 
GROUP BY art.name 
ORDER thi.createdAt DESC

node to traverse cannot be null!

This is a generic Hibernate error message indicating a syntax problem in your query. As another example, forgetting to start a select clause with the word "SELECT" would yield the same error.

In this instance the syntax error is due to the on clause - HQL does not support them. Instead do a cross join like so:

FROM track_history_items thi, artists art 
WHERE thi.type = "TrackBroadcast" 
AND  thi.artist_id = art.id 
GROUP BY art.name 
ORDER thi.createdAt DESC
寂寞陪衬 2024-11-10 20:10:21

我之前也多次遇到过这个问题,而且代码总是尝试通过调用 createQuery 而不是 createNamedQuery 来运行命名查询,例如如果您有一个名为 "FIND_XXX" 的命名查询,则代码将调用 entityManager.createQuery(FIND_XXX) ,这会导致它尝试执行表示该名称的字符串命名查询就好像它是一个标准的动态查询字符串(这显然是一个问题)。

I have come across this issue several times before as well and it has always been the case that the code was attempting to run a named query by calling createQuery instead of createNamedQuery, e.g. if you have a named query named "FIND_XXX" then the code would be calling entityManager.createQuery(FIND_XXX) which results in it trying to execute the String representing the name of the named query as if it was a standard dynamic query String (which is obviously a problem).

故事↓在人 2024-11-10 20:10:21

如果执行的查询存在语法错误,例如忘记更新语句中的逗号,您也会收到此错误。

update MyObject set field1=5 field2=4 WHERE id = 4

查看 field1=5 和 field2=4 之间如何缺少逗号?您将得到要遍历的节点不能为空错误。

You can also get this error if you execute a query that has a syntax error, such as forgetting a comma in an update statement

update MyObject set field1=5 field2=4 WHERE id = 4

See how there is a missing comma between field1=5 and field2=4? You will get a node to traverse cannot be null error.

何以畏孤独 2024-11-10 20:10:21

这个错误通常是由于人们甚至无法想象的最愚蠢的原因之一造成的。如果您复制粘贴查询,则会引入一些特殊字符,并且您开始收到此错误。确保您手动输入查询,它将正常工作。至少在我的例子中它有效。

This error comes usually due to one of the most stupid reason that one would not have even imagined. If you dop copy-paste the query, some special characters get introduced and you start getting this error. Make sure you type the query manually and it will work fine. Atleast in my case it worked.

痴者 2024-11-10 20:10:21

如果您使用 Hibernate 4,您应该调用:

Query query = session.getNamedQuery(Entity.NAMED_QUERY);

而不是

Query query = session.createQuery(Entity.NAMED_QUERY);

发生这种情况是因为 session.createQuery 将尝试根据 Entity.NAMED_QUERY 的字符串值创建查询:

String NAMED_QUERY = "Entity.namedQuery";

导致 HQL 不正确并引发异常。

If you are using Hibernate 4 you should be calling:

Query query = session.getNamedQuery(Entity.NAMED_QUERY);

instead of

Query query = session.createQuery(Entity.NAMED_QUERY);

This happens because session.createQuery will try to create the query based on the string value of Entity.NAMED_QUERY for example:

String NAMED_QUERY = "Entity.namedQuery";

resulting in a incorrect HQL and raising a exception.

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