gpt4 book ai didi

java - hibernate中要遍历的节点不能为空异常

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:23:02 24 4
gpt4 key购买 nike

我一直在尝试使用 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]

这是我的 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();
}
}

}

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

提前致谢!!

最佳答案

问题,显然是这个

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

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

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

关于java - hibernate中要遍历的节点不能为空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36954770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com