问题描述
我是新手,刚学Hirbernate抄的程序(北京尚学堂-马士兵),运行出现的问题,望大虾们赐教:这是运行的程序packagecom.bjsxt.hibernate.modle;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.AnnotationConfiguration;importorg.junit.AfterClass;importorg.junit.BeforeClass;importorg.junit.Test;publicclassTeacherTest{privatestaticSessionFactorysf=null;@BeforeClasspublicstaticvoidbeforeClass(){System.out.println("eeeeeeeeeeeee");sf=newAnnotationConfiguration().configure().buildSessionFactory();System.out.println("ffffffffffffff");}@TestpublicvoidtestTeacherSave(){System.out.println("gggggggggggggg");Teachert=newTeacher();t.setId(2);t.setName("t2");t.setTitle("中级");Sessionsession=sf.openSession();session.beginTransaction();session.save(t);session.getTransaction().commit();System.out.println("cccccccccccccccc");session.close();System.out.println("dddddddddddddddddd");}publicstaticvoidmain(String[]args){System.out.println("hhhhhhhhhhhh");beforeClass();}@AfterClasspublicstaticvoidafterClass(){System.out.println("aaaaaaaaaa");sf.close();System.out.println("bbbbbbbbbbbb");}}这是运行后在CONSOLE界面显示的内容eeeeeeeeeeeeeaaaaaaaaaa这是在JUJUnit界面显示的内容TeacherTestcom.bjsxt.hibernate.modle.TeacherTestjava.lang.NullPointerExceptionatcom.bjsxt.hibernate.modle.TeacherTest.afterClass(TeacherTest.java:44)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:597)atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)atorg.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)atorg.junit.runners.ParentRunner.run(ParentRunner.java:236)atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
解决方案
解决方案二:
sf=newAnnotationConfiguration().configure().buildSessionFactory();sf没有实例化,所以在afterClass里面sf会有空指针错误。把sf=newAnnotationConfiguration().configure().buildSessionFactory();用trycatch包围一下再调试
解决方案三:
2楼那个也是一个调试的办法。。。你这个里的代码没有出错估计就是你配置里面那里写错了。。。。把sf=newAnnotationConfiguration().configure().buildSessionFactory();trycatch一下估计就有明显的错误了。。。。你用junit有会就是有那个不报错的bug.
解决方案四:
你就再顺带的把spring加进来算咯用它来管你的什么sessionFactory和session什么的事务也是
解决方案五:
这个try/catch我试过了。这是在JUnit环境下运行的显示:eeeeeeeeeeeeeaaaaaaaaaa这是用runas运行后的显示:hhhhhhhhhhhheeeeeeeeeeeeeExceptioninthread"main"java.lang.IncompatibleClassChangeError:Implementingclassatjava.lang.ClassLoader.defineClass1(NativeMethod)atjava.lang.ClassLoader.defineClass(ClassLoader.java:621)atjava.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)atjava.net.URLClassLoader.defineClass(URLClassLoader.java:260)atjava.net.URLClassLoader.access$000(URLClassLoader.java:56)atjava.net.URLClassLoader$1.run(URLClassLoader.java:195)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.net.URLClassLoader.findClass(URLClassLoader.java:188)atjava.lang.ClassLoader.loadClass(ClassLoader.java:307)atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)atjava.lang.ClassLoader.loadClass(ClassLoader.java:252)atjava.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)atcom.bjsxt.hibernate.modle.TeacherTest.beforeClass(TeacherTest.java:21)atcom.bjsxt.hibernate.modle.TeacherTest.main(TeacherTest.java:49)“java.lang.IncompatibleClassChangeError”这个错误语句我在网上搜过了,没找到解决方法
解决方案六:
sf=newAnnotationConfiguration().configure().buildSessionFactory();不仅sf没有实例化,下面的testTeacherSave()方法也没有执行。这个问题挺怪的
解决方案七:
哈哈,你跟着他做肯定不行啦,因为你runas的时候,是测试testTeacherSave()方法,但是beforeClass()方法根本还没执行,所以你要测试通过,一定要整个类测试,明白了没?
解决方案八:
你用的hibernate的什么版本的3.2?sf=newAnnotationConfiguration().configure().buildSessionFactory();??不是sf=newConfiguration().buildSessionFactory();??
解决方案九:
谢谢大家的关心,这个问题已经解决,积分系统会自动分配给大家,因为这个BUG我也忘了是怎么解决的了,这里也祝大家新年快乐!