node to traverse cannot be null (Hibernate SQL)

前端 未结 6 1478
醉酒成梦
醉酒成梦 2021-02-12 03:23

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 t         


        
相关标签:
6条回答
  • 2021-02-12 04:03

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

    0 讨论(0)
  • 2021-02-12 04:04

    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.

    0 讨论(0)
  • 2021-02-12 04:05

    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
    
    0 讨论(0)
  • 2021-02-12 04:12

    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");
    
    0 讨论(0)
  • 2021-02-12 04:12

    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.

    0 讨论(0)
  • 2021-02-12 04:19

    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.

    0 讨论(0)
提交回复
热议问题