今天在进行Struts、Spring、Hibernate的整合配置时,碰到一系列的问题,其中有一个是:
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)
刚开始忙活了半天,没能解决,后来在网上找到了一篇博文,地址如下:http://www.blogjava.net/sxyx2008/archive/2009/10/19/298821.html?opt=admin
以下部分内容转载上面:
从上面的异常信息可以看出是在Hibernate进行save的时候检测到bean为null
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: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>
<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中模型驱动对象的名字一致否则就会报错。
按照上述方法解决之后,发现还是不得行,出现下面的错误:
Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES)); nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))
后来仔细检查了applicationContext.xml文件后发现,原来是属性密码设置错了,本来应该为123,我居然设置成root了,改过之后,就能正常连接数据库了。