Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node to traverse cannot be null exception in hibernate

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

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

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 !!

like image 413
Abhijeet Kale Avatar asked Feb 07 '23 03:02

Abhijeet Kale


1 Answers

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

Query q = session.createQuery("from Menu where menuid = :menuid");
q.setParameter("menuid", menuid);
like image 51
v.ladynev Avatar answered Feb 13 '23 03:02

v.ladynev