java 在休眠中要遍历的节点不能为空异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36954770/
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-11-03 02:02:53  来源:igfitidea点击:

node to traverse cannot be null exception in hibernate

javaspringhibernate

提问by Abhijeet Kale

I have been trying CRUD operation using spring and hibernate but get following exception.

我一直在尝试使用 spring 和 hibernate 进行 CRUD 操作,但遇到以下异常。

java.lang.IllegalArgumentException: node to traverse cannot be null!
    at org.hibernate.hql.internal.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:64) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:300) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    at com.kimaya.webpanel.dao.MenuDAOImpl.getMenu(MenuDAOImpl.java:51) ~[MenuDAOImpl.class:na]
    at com.kimaya.webpanel.dao.MenuDAOImpl.removeMenu(MenuDAOImpl.java:87) ~[MenuDAOImpl.class:na]
    at com.kimaya.webpanel.service.MenuServiceImpl.removeMenu(MenuServiceImpl.java:48) ~[MenuServiceImpl.class:na]
    at com.kimaya.webpanel.web.controller.MenuController.removeMenu(MenuController.java:70) ~[MenuController.class:na]

Here is my MenuDAOImpl class

这是我的 MenuDAOImpl 类

package com.kimaya.webpanel.dao;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kimaya.webpanel.model.Menu;
import com.kimaya.webpanel.model.User;

@Repository("menuDAO")
public class MenuDAOImpl implements MenuDAO {

    protected static Logger logger = LoggerFactory.getLogger(MenuDAO.class);

    @Autowired
    protected SessionFactory sessionFactory;


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void addMenu(Menu menu) {
        Session session = sessionFactory.openSession();
        String parentmenu = menu.getMenuname();
        String menuname[] = menu.getMenuname().split(" ");
        int parentid = Integer.parseInt(menuname[0]);
        //Query q = session.createQuery("from "+Menu.class.getName()+" where menuname="+menu.getMenuname());
        menu.setParentid(parentid);
        String mname[] = parentmenu.split(",");
        menu.setMenuname(mname[1]);
        session.save(menu);
    }

    public Menu getMenu(Integer menuid) {
        Session session=null;
        Transaction transaction = null;
        List<Menu> menus = null;
        try{
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            Query q = session.createQuery("from" + Menu.class.getName() + "where menuid="+menuid);
             menus = q.list();
             return menus.get(0);
        }
        catch(HibernateException e) {
            transaction.rollback();
        } finally {
            session.close();
        }
        return menus.get(0);

    }

    public void updateMenu(Menu menu) {
        Session session = sessionFactory.openSession();
        session.update(menu);
    }



    public List<Menu> menuList(){
        Session session = sessionFactory.openSession();
        //Query q = session.createQuery("select g from " + Menu.class.getName() + " g");
        //List<Menu> menulist = q.list();

        List<Menu> menulist = session.createQuery("from "+Menu.class.getName()).list();

        return menulist;
    }

    public void removeMenu(Integer menuid) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.delete(this.getMenu(menuid));
            transaction.commit();
        } catch(HibernateException e) {
            transaction.rollback();
        } finally {
            session.close();
        }   
    }

}

I can not able to understand why this exception comes and how to resolve it. Please help me.

我无法理解为什么会出现此异常以及如何解决它。请帮我。

Thanks in advance !!

提前致谢 !!

回答by v.ladynev

The problem,obviously with this

问题,显然是这样的

Query q = session.createQuery("from" + Menu.class.getName() + "where menuid="+menuid);

You miss spaces. And, please, use parameters. For an example, in most situations, a parametrized query has a better performance (and doesn't have SQL injections).

你想念空格。并且,请使用参数。例如,在大多数情况下,参数化查询具有更好的性能(并且没有 SQL 注入)。

Query q = session.createQuery("from Menu where menuid = :menuid");
q.setParameter("menuid", menuid);

回答by pintu

This type of problem occurred just because of syntax error.one example for this is shown below

发生这种类型的问题只是因为语法错误。一个例子如下所示

If you are missing from clause then also found the same issues

如果您缺少 from 子句,则也发现了相同的问题

Wrong Query-

错误查询-

String query=" user where lid=? and lmt=?";

Right Query-

正确查询-

String query="from user where lid=? and lmt=?";