温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
 

今天在写struts2 save的时候报了这么一个异常

java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity

    at org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:40)

    at org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:23)

    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)

    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)

从上面的异常信息可以看出是在Hibernate进行save的时候检测到beannull

1)        大致从这几个方面来分析原因:

2)        Hibernate(*.hbm.xml)映射文件是否错误

3)        DAO层代码是否错误

4)        检查Action及视图层jsp页面等

从上面几个逐个检查后得出的结论为第四条

首先看Action中的代码

private Employee employee;

    public Employee getEmployee() {

        returnemployee;

    }

    publicvoid setEmployee(Employee employee) {

        this.employee = employee;

    }

这里employee是我们观察的重点

再看jsp中的代码

<s:form action="saveEmployee.action">

        <s:textfield name="empName" label="empName"></s:textfield>

        <s:textfield name="age" label="age"></s:textfield>

        <s:textfield name="sex" label="sex"></s:textfield>

        <s:textfield name="context" label="context"></s:textfield>

        <s:submit value="注册" theme="simple"></s:submit>

        <s:reset value="取消" theme="simple"></s:reset>

</s:form>

大家注意<s:textfield/>name属性的值

很显然错误发生在此处修改如下即可:

<s:form action="saveEmployee.action">

        <s:textfield name="employee.empName" label="empName"></s:textfield>

        <s:textfield name="employee.age" label="age"></s:textfield>

        <s:textfield name="employee.sex" label="sex"></s:textfield>

        <s:textfield name="employee.context" label="context" value=""></s:textfield>

        <s:submit value="注册" theme="simple"></s:submit>

        <s:reset value="取消" theme="simple"></s:reset>

    </s:form>

由此我们得出的结论是:

jsp页面中一定要保持页面中各个控件的name属性与Action中模型驱动对象的名字一致否则就会报错

posted on 2009-10-19 11:39 雪山飞鹄 阅读(27929) 评论(19)  编辑  收藏 所属分类: Hibernate

Feedback

# re: attempt to create saveOrUpdate event with null entity 2010-11-07 16:18 smallmoon
大哥,你帮了我一个大忙啊,真是,我也是遇到了这个问题。拜读了你的文章之后,豁然开朗,谢谢哈  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2011-02-25 16:10 全球
我已经这样写了 可还是这个错误啊  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2012-03-15 15:28 someone
很给力!!!谢谢!!  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2012-04-03 22:30 二万人
解决了我的问题,谢谢  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2012-05-16 16:03 dgd
怎么回事啊,我有两个页面添加,都是用的模型驱动啊,有一个加入了数据,还有一个报这个错,不能加入数据,怎么有的解决的了,有的解决不了呢。知道底层是怎么实现的吗。  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2012-05-16 16:21 dgd
这样还真解决了,谢谢。可是我还是不太清楚。我第一个页面的时候,也是这样写的啊,也是用的模型驱动。是可以把数据加入进去的。第二个页面加入数据的时候就不行了。用了你上面的方法才解决的。真不知道是为什么?要是可以看底层代码的话。应该就可以找出是哪里出问题了。  回复  更多评论
  

# 过客[未登录] 2012-09-07 16:56 1
真的很感谢你,问题解决了……  回复  更多评论
  

# 真给力的工具[未登录] 2013-03-26 17:28
真的很给力,特别在spring中使用。。  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2013-11-23 16:12 ilex
因为employee是一个对象@dgd
  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity[未登录] 2013-11-23 16:15 candy
爱死你了!!!  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2014-02-20 18:13 sdfwewerw
我也遇到了,也是忘了加"对象."。  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity[未登录] 2014-04-03 09:25 w
有用,非常感谢。  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2014-04-27 20:31 多大的
你这个问题解决了吗,我跟你一样啊@全球
  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2014-09-25 10:38 chenbin
谢谢啦啦啦啦。  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2015-01-24 16:40 pei
一样的,但是没解决问题  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity[未登录] 2015-04-18 20:55 张云
大哥。你的文章拯救了我啊。我这几天在做毕业论文,打了一个Spring+hibernate+Strus2框架,可是一测试怎么出现此问题,找了几天了找不出,郁闷死了。多亏看见你的文章了,总之,谢谢了。呵呵  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity[未登录] 2016-02-24 10:45 shi
没有解决问题  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity[未登录] 2016-03-08 15:11 影子
@全球
<property name="price" type="java.lang.Double">
<column name="price" not-null="true">
注意not_null="true",这个地方如果你赋值的时候给了一个空值 也会出这个异常  回复  更多评论
  

# re: attempt to create saveOrUpdate event with null entity 2016-05-29 18:38 余糖糖
没有用,配置文件什么都是好的,写一个测试类添加数据就可以,但是一到jsp页面里面就不可以  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: