Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带hibernate的Spring在出现异常错误后不刷新会话_Spring_Hibernate_Flush - Fatal编程技术网

带hibernate的Spring在出现异常错误后不刷新会话

带hibernate的Spring在出现异常错误后不刷新会话,spring,hibernate,flush,Spring,Hibernate,Flush,我将Spring事务管理与Hibernate结合使用。我的服务层方法是事务性的。当用户试图创建具有现有ID的公司时,在测试用例时发生此错误。以下是我的控制器: @RequestMapping(value = "/CreateCompany", method = RequestMethod.POST) public @ResponseBody String createUser(HttpServletRequest request) { String response = null;

我将Spring事务管理与Hibernate结合使用。我的服务层方法是事务性的。当用户试图创建具有现有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;

}
事务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>

需要传播\u,-java.lang.Exception
需要传播\u,-java.lang.Exception
需要传播\u,-java.lang.Exception
传播_支持,只读,-java.lang.Exception
传播_支持,只读,-java.lang.Exception
传播_支持,只读,-java.lang.Exception
在我的JSP中,我检查如果我的响应是d:false(我在getUtils().toKendoResponse(false)中创建),那么我会向用户显示一个警报“注册中存在一些问题”

但是,一旦在服务层保存期间出现错误(重复密钥错误),代码将到达服务的末尾。之后,控制器中会出现另一个错误:

@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;
}
ERROR CompanyController:47-错误:com.ig.avs.common.entity.db.Company条目中的服务URL/CreateCompany;方法createUser(..)null id(发生异常后不刷新会话)

控制器将“response”返回为null

我已经尝试删除我在dao层中正在进行的刷新,但没有任何帮助。我在网站上查看了关于此问题的其他答案,但其中大多数人在出现错误后没有返回。我正在捕获我的异常并从我的服务返回,因此不确定另一个刷新发生在哪里


任何输入都会有很大的帮助!

您正在使用Spring进行事务管理(以一种非常古老的skool方式),在服务方法结束后,事务被提交,会话被刷新。因此,不要在服务中捕获异常,而要在控制器中捕获它

还有几件事

  • 不要使用
    HibernateDaoSupport
    HibernateTemplate
    自hibernate 3.0.1(2006年左右)起应被视为不推荐使用的工具,只需直接使用
    SessionFactory
  • 不要使用
    TransactionProxyFactoryBean
    只需使用
    @Transactional
    注释您的服务,然后使用
    ,这将为您省去很多麻烦
  • 为什么不利用Spring映射到JSON或从JSON映射到JSON,而是自己在服务层中进行映射,服务层现在直接绑定到web层
  • 从您的服务方法和dao调用来看,您在实体映射方面也遇到了问题。为什么要手动设置链接,hibernate可以以这样一种方式处理,您只需要存储
    公司

  • 谢谢!我从服务中抛出了错误,并在控制器中捕获了它,我终于得到了我所需要的。遗憾的是,我对J2EE世界还很陌生,所以我似乎遇到了一些问题。我将尝试您的其他建议。再次感谢。。