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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5817775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:20:20  来源:igfitidea点击:

node to traverse cannot be null (Hibernate SQL)

sqlhibernatenull

提问by tiswas

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

我正在通过休眠执行 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

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?

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

回答by MariemJab

I have got that error only because I was declaring a createQueryinstead of createNamedQuery. So When detecting this, hibernate throw the exception

我得到这个错误只是因为我声明了一个createQuery而不是createNamedQuery。所以当检测到这一点时,休眠抛出异常

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

回答by Evan Oxfeld

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.

这是一条通用的 Hibernate 错误消息,表明您的查询存在语法问题。再举一个例子,忘记用“SELECT”这个词开始一个选择子句会产生同样的错误。

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

在这种情况下,语法错误是由于 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

回答by brent777

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 createQueryinstead 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).

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

回答by Gandalf

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.

看看如何在 field1=5 和 field2=4 之间缺少逗号?你会得到一个要遍历的节点不能为空的错误。

回答by user1842915

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.

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

回答by Samuel Torga

If you are using Hibernate 4 you should be calling:

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

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

instead of

代替

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

This happens because session.createQuerywill try to create the query based on the string value of Entity.NAMED_QUERYfor example:

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

String NAMED_QUERY = "Entity.namedQuery";

resulting in a incorrect HQL and raising a exception.

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