Hibernate异常:IllegalArgumentException

Posted GEORES

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate异常:IllegalArgumentException相关的知识,希望对你有一定的参考价值。

异常信息:

 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的主要内容,如果未能解决你的问题,请参考以下文章

无法提交 Hibernate 事务;嵌套异常是 org.hibernate.Transaction 异常:JDBC 提交失败

hibernate异常:org.hibernate.exception.GenericJDBCException

Hibernate 异常总结

org.hibernate.lazyinitialization 异常

Hibernate异常:IllegalArgumentException

Hibernate 异常提示_1