Hibernate異常:IllegalArgumentException
阿新 • • 發佈:2017-09-10
org delete eclipse block illegal create str eva class
異常信息:
1 java.lang.IllegalArgumentException: attempt to create delete event with null entity 2 at org.hibernate.event.spi.DeleteEvent.<init>(DeleteEvent.java:31) 3 at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:860) 4 at com.geore.test.TestDemo4.fun2(TestDemo4.java:77)5 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 6 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 7 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 8 at java.lang.reflect.Method.invoke(Method.java:601)9 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 10 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 11 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 12 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)13 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 14 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69) 15 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48) 16 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 17 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 18 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 19 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 20 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 21 at org.junit.runners.ParentRunner.run(ParentRunner.java:292) 22 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 23 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 24 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 25 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 26 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 27 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
錯誤代碼:
1 // 第一步:查找用戶 2 Users user = session.get(Users.class, "4028d4015e6ebff8015e6ebffffd0004"); 3 session.delete(user);
異常原因:
從異常信息的顯示來看,很容易看出來產生異常的原因,是因為存在null對象,切這個對象在調用Hibernate的Session類的delete()方法。那麽很顯然user對象為空,Hibernate沒有通過get()方法查找到Users對象。那麽這個原因就是處在給的查詢條件是不存在的,因此是id在數據庫中不存在,因此找不到此條記錄,因此出現異常。將id改成一個存在的值即可。
正確的解決代碼:
1 // 第一步:查找用戶 2 Users user = session.get(Users.class, "4028d4015e6ebff8015e6ebfffa60000"); 3 session.delete(user);
Hibernate異常:IllegalArgumentException