Eclipse Jee-indigo + Hibernate 3.2 + JDK 1.6.31
Hibernate load某个对象出现如下异常:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.oracle.entities.Dictionary#1] at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:419) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:154) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:143) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) at com.oracle.entities.Dictionary_$$_javassist_0.getName(Dictionary_$$_javassist_0.java) at com.oracle.entities.HBTest.testMapLoad(HBTest.java:130) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
报这个异常很大原因是该对象不存在,在testMap这个方法里保存数据,在testMapLoad这个方法里加载数据,思考下为什么数据会不存在。原来是在Dictionary.hbm.xml这个配置文件中做了如下配置:
<property name="hbm2ddl.auto">create</property>
这样配置后就会出现每次执行操作都会重新创建表,在testMapLoad里只加载没插入数据自然就找不到了。于是解决办法这样:先执行testMap这个方法保存数据,然后把
<property name="hbm2ddl.auto">create</property>改成
<property name="hbm2ddl.auto">update</property>,再重新运行testMapLoad自然就不会报此异常了。
//代码片段 @Test public void testMap(){ Map words=new HashMap(); words.put("hello", "你好"); words.put("ok", "是"); Dictionary dic=new Dictionary(); dic.setName("eng"); dic.setPublisher("william"); dic.setWords(words); session.save(dic); } @Test public void testMapLoad(){ Dictionary dic = new Dictionary(); dic = (Dictionary)session.load(Dictionary.class, 1); System.out.println(dic.getName()); Map words=new HashMap(); words = dic.getWords(); System.out.println(words.isEmpty()); System.out.println(words.get("ok")); }
http://yushengxiang.iteye.com/blog/818829
http://wj98127.iteye.com/blog/189723