要遍历的节点不能为空(Hibernate SQL)
我正在通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
仅当使用
createQuery
而不是createNamedQuery
时,我才收到该错误。因此,当检测到这一点时,hibernate 会抛出异常。
I have gotten that error only when using
createQuery
instead ofcreateNamedQuery
.So when detecting this, hibernate throws the exception.
这是一条通用的 Hibernate 错误消息,指示您的查询中存在语法问题。作为另一个示例,忘记以单词“SELECT”开始 select 子句会产生相同的错误。
在这种情况下,语法错误是由于 on 子句造成的 - HQL 不支持它们。相反,像这样进行交叉连接:
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:
我之前也多次遇到过这个问题,而且代码总是尝试通过调用
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 ofcreateNamedQuery
, e.g. if you have a named query named"FIND_XXX"
then the code would be callingentityManager.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).如果执行的查询存在语法错误,例如忘记更新语句中的逗号,您也会收到此错误。
查看 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
See how there is a missing comma between field1=5 and field2=4? You will get a node to traverse cannot be null error.
这个错误通常是由于人们甚至无法想象的最愚蠢的原因之一造成的。如果您复制粘贴查询,则会引入一些特殊字符,并且您开始收到此错误。确保您手动输入查询,它将正常工作。至少在我的例子中它有效。
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.
如果您使用 Hibernate 4,您应该调用:
而不是
发生这种情况是因为
session.createQuery
将尝试根据Entity.NAMED_QUERY
的字符串值创建查询:导致 HQL 不正确并引发异常。
If you are using Hibernate 4 you should be calling:
instead of
This happens because
session.createQuery
will try to create the query based on the string value ofEntity.NAMED_QUERY
for example:resulting in a incorrect HQL and raising a exception.