问题描述
eclipse使用Hibernate出错java.lang.ClassNotFoundException调试了一天了,各位帮帮忙啊出错信息如下:java.lang.NoClassDefFoundError:org/dom4j/DocumentExceptionorg.util.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:57)org.util.HibernateSessionFactory.getSession(HibernateSessionFactory.java:45)org.dao.imp.ChapterDaoImp.getChapterByID(ChapterDaoImp.java:22)org.action.ChapterAction.execute(ChapterAction.java:21)sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)sun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)java.lang.reflect.Method.invoke(UnknownSource)HibernateSessionFactory类的代码:packageorg.util;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;publicclassHibernateSessionFactory{privatestaticStringCONFIG_FILE_PATH="/hibernate.cfg.xml";privatestaticStringconfigFile=CONFIG_FILE_PATH;//创建一个线程局部保存对象privatestaticfinalThreadLocal<Session>threadLocal=newThreadLocal<Session>();privatestaticConfigurationconfiguration;//=newConfiguration();privatestaticSessionFactorysessionFactory;privateHibernateSessionFactory()throwsException{super();}publicstaticSessiongetSession(){Sessionsession=threadLocal.get();if(session==null||!session.isOpen()){if(sessionFactory==null)rebuildSessionFactory();session=(sessionFactory!=null)?sessionFactory.openSession():null;//将Session对象绑定到当前线程上面threadLocal.set(session);}returnsession;}publicstaticvoidrebuildSessionFactory(){try{configuration=newConfiguration().configure(configFile);//这里出错了。。。。sessionFactory=configuration.buildSessionFactory();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidcloseSession(){Sessionsession=threadLocal.get();threadLocal.set(null);if(session!=null)session.close();}publicstaticConfigurationgetConfiguration(){returnconfiguration;}publicstaticSessionFactorygetSessionFactory(){returnsessionFactory;}publicstaticvoidsetConfigFile(StringconfigFile){HibernateSessionFactory.configFile=configFile;sessionFactory=null;}}
配置文件:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/HibernateConfigurationDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factoryname="HibernateSessionFactory"><propertyname="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property><propertyname="hibernate.connection.password">123456</property><propertyname="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost;DatabaseName=JavaEECoursework;SelectMethod=cursor</property><propertyname="hibernate.connection.username">sa</property><propertyname="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property><propertyname="hibernate.show_sql">true</property><mappingclass="org.model.Chapter"/></session-factory></hibernate-configuration>
到底哪里错了吖。。。。。各位帮帮忙啊,感激不尽,调试到崩溃了TAT。。。
解决方案
解决方案二:
http://dom4j.sourceforge.net/下载dom4j
解决方案三:
引用1楼的回复:
http://dom4j.sourceforge.net/下载dom4j
谢谢,不过下载了那个包之后又有错误了java.lang.NullPointerExceptionorg.dao.imp.ChapterDaoImp.getChapterByID(ChapterDaoImp.java:23)org.action.ChapterAction.execute(ChapterAction.java:21)sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)sun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)
调用HibernateFactory的代码packageorg.dao.imp;importorg.dao.ChapterDao;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.model.Chapter;importorg.util.HibernateSessionFactory;publicclassChapterDaoImpimplementsChapterDao{privateSessionFactorysessionFactory;publicvoidsetSessionFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}publicChaptergetChapterByID(IntegerchapterID){Sessionsession=HibernateSessionFactory.getSession();Transactionts=session.beginTransaction();Queryquery=session.createQuery("fromChapterwhereChapterID=?");query.setParameter(0,chapterID);query.setMaxResults(1);Chapterchapter=(Chapter)query.uniqueResult();ts.commit();HibernateSessionFactory.closeSession();returnchapter;}}
解决方案四:
我也遇到一样的问题!!一起顶!!
解决方案五:
ClassNotFoundException找不到类异常缺少hibernate的jar包或hibernate的jar包与其他包有冲突,看看hibernate*.jar有几个
解决方案六:
Transactionts=session.beginTransaction();session没获取成功。
解决方案七:
session是null,看配置文件