1.首先简要介绍一下出现的问题的环境
jdk1.6+strut2.0.11.1+Spring2.5+hibernate3.2.6.ga
使用了SpringOpenEntityManagerInViewFilter作为lazyloading.
2.问题出现的步骤和相关代码
主要问题:在action的某个方法中执行完添加数据的操作(insert)后返回success后跳转到添加页面后莫名其妙的又执行了一个update操作,把我插入数据库的数据给修改了.
代码如下
进入添加页面前的action方法
public String contractorsaSignup() throws Exception
{
if (apparelhrContractor == null) {
apparelhrContractor = new ApparelhrContractor();
apparelhrContractor.setContractorTitle("test");
apparelhrContractor.setContractorDescription("test");
apparelhrContractor.setRegistrationDate(Globals.getCurrentSqlDate());
apparelhrContractor.setPostDate(apparelhrContractor.getRegistrationDate());
}
return SUCCESS;
}
插入数据库数据的action方法
public String contractorsaCreate() throws Exception {
if (apparelhrContractor != null) {
apparelhrContractor.setContractorTitle("");
apparelhrContractor.setContractorDescription("");
apparelhrContractorService.addApparelhrContractor(apparelhrContractor);
String resultMessage = getText("contractorsa.success");
addActionMessage(resultMessage);
}
return SUCCESS;
}
struts.xml配置
<action name="*Front" class="contractorAction" method="{1}" >
<result>/contractorsaSignup.jsp</result>
</action>
以上两个action方法都跳转到同一个页面contractorsaSignup.jsp
我的本意是想把页面提交上来数据中的ContractorTitle和ContractorDescription字段人为清空.
结果执行完insert操作跳转到contractorsaSignup.jsp页面后又把已经清空额ContractorTitle,ContractorDescription数据给update回去了.
后台的log输出如下
Hibernate: insert into apparelhr_contractor (city, company_brief, company_name, contact_persion, contractor_description, contractor_title, country, email, mobile, post_date, registration_date, service_offered, street, suburb, telephone) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2008-5-9 11:12:49 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action [/include/contractorSubMenu!execute] took 16 ms.
2008-5-9 11:12:49 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action [//contractorsaCreateFront!contractorsaCreate] took 828 ms.
Hibernate: select apparelhrl0_.link_id as link1_6_, apparelhrl0_.content as content6_, apparelhrl0_.create_date as create3_6_, apparelhrl0_.sequence as sequence6_, apparelhrl0_.title as title6_, apparelhrl0_.url as url6_ from apparelhr_link apparelhrl0_ order by apparelhrl0_.sequence
Hibernate: update apparelhr_contractor set city=?, company_brief=?, company_name=?, contact_persion=?, contractor_description=?, contractor_title=?, country=?, email=?, mobile=?, post_date=?, registration_date=?, service_offered=?, street=?, suburb=?, telephone=? where contractor_id=?
大家可以看到执行完insert回到contractorsaSignup.jsp后又执行了个update语句.
但是如果我把
apparelhrContractor.setContractorTitle("");
apparelhrContractor.setContractorDescription("");
给去掉就不会又update语句出现了.
实在是不知道为什么会出现这种奇怪现象,还请兄弟们给指点指点.