【发布时间】:2013-11-19 10:51:58
【问题描述】:
我在 Hibernate 中使用 Spring 事务管理。我的服务层方法是事务性的。当用户尝试创建具有现有 ID 的公司时,在测试用例时会发生此错误。这是我的控制器:
@RequestMapping(value = "/CreateCompany", method = RequestMethod.POST)
public @ResponseBody
String createUser(HttpServletRequest request) {
String response = null;
try {
response = getUserServiceTransaction().save(request);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(
"ERROR: service URL /CreateCompany; " +
"method createUser(..)"+
e.getMessage());
}
}
return response;
}
服务保存方式:
@覆盖 公共字符串保存(HttpServletRequest 请求)抛出 ServiceException {
String requestJson = null;
try {
requestJson = getUtils().fetchJsonRequestString(request);
Company company = (Company) getUtils().fromJson(requestJson,
Company.class);
String companyId = company.getCompanyId();
getCompanyDao().save(company);
List<String> linkedCompanies = null;
String parentCompanyId = company.getCompanyId();
linkedCompanies = company.getLinkedCompanies();
Iterator<String> iterator = linkedCompanies.iterator();
while (iterator.hasNext()) {
CompanyLinkage companyLinkage = new CompanyLinkage();
companyLinkage.setParentCompanyId(parentCompanyId);
companyLinkage.setChildCompanyId(iterator.next());
getCompanyLinkageDao().save(companyLinkage);
}
requestJson = getUtils().toKendoResponse(true);
}
catch (Exception e) {
logger.error("Error:" + e);
requestJson = getUtils().toKendoResponse(false);
}
return requestJson;
}
我的 DAO:
公共类 CompanyDaoImpl 扩展 HibernateDaoSupport 实现 CompanyDao{
@Override
public void save(Company company) throws DAOException {
try{
getHibernateTemplate().save(company);
getHibernateTemplate().flush();
}
catch(Exception e){
throw new DAOException(e);
}
}
事务性 bean:
<!-- User Service -->
<bean id="userService" class="com.ig.avs.adapters.db.service.UserService">
<property name="companyDao" ref="companyDao"/>
<property name="companyLinkageDao" ref="companyLinkageDao"/>
<property name="utils" ref="utils" />
</bean>
<bean id="userServiceTransaction"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager" />
<property name="target" ref="userService" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly , -java.lang.Exception</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly ,-java.lang.Exception</prop>
<prop key="list*">PROPAGATION_SUPPORTS, readOnly ,-java.lang.Exception</prop>
</props>
在我的 JSP 中,我正在检查我的响应是否为 d:false(我在 getUtils().toKendoResponse(false) 中创建),然后我会向用户显示警告“注册中存在一些问题”
但是一旦在服务层的保存过程中出现错误(重复键错误),代码就会到达服务的末尾。之后控制器中出现另一个错误:
错误 CompanyController:47 - 错误:服务 URL /CreateCompany; com.ig.avs.common.entity.db.Company 条目中的方法 createUser(..)null id(发生异常后不要刷新 Session)
并且控制器将“响应”返回为空。
我已尝试删除我在 dao 层中执行的刷新,但没有帮助。我已经在网站上查看了有关此问题的其他答案,但其中大多数人在出错后没有返回。我发现我的异常并从我的服务中返回,所以不确定另一个刷新发生在哪里。
任何意见都会有很大帮助!
【问题讨论】: