【问题标题】:HibernateTransactionManager + Spring + update() = Session not flushingHibernateTransactionManager + Spring + update() = 会话不刷新
【发布时间】:2014-07-09 19:12:47
【问题描述】:

我对所有这些 hibernate+spring 的东西都很陌生,在一个真实的项目中学习它(是的,我喜欢这样做)......

在我的 applicationcontext.xml 中,我设置了 sessionfactory 和 transactionManager(均使用默认名称)。

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/database.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />


<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven/>    

我的控制器方法使用 usuarioService 自动装配并工作。我得到了填写表格的@modelattribute Usuario。我也在解析到 usuarioService 记录的用户 ID(我正在做测试,所以请随时告诉我控制器中的最佳方法)

@RequestMapping(value="/atualizaCadastro",method = RequestMethod.POST)
public String updateCadastro(@ModelAttribute("usuario") Usuario usuario, HttpSession session){
    Usuario usuarioLogado = (Usuario)session.getAttribute("usuarioLogado");
    usuarioService.updateUsuario(usuarioLogado.getId(),usuario);
    return "redirect:home";
}

我的服务层在其方法中带有 @transactional 注释,usuarioDAO 是自动装配的...

@Transactional
public void updateUsuario(Long usuarioId, Usuario usuarioUpdate) {
    // TODO Auto-generated method stub
    usuarioDAO.updateUsuario(usuarioId,usuarioUpdate);
}

我得到了我的 DAO 方法:

@Override
public void updateUsuario(Long usuarioId, Usuario usuarioUpdate) {
    // TODO Auto-generated method stub
    Session sessao = getSession().getCurrentSession();
    Usuario usuario = (Usuario)sessao.load(Usuario.class, usuarioId);
    usuario.setCelular(usuarioUpdate.getCelular());
    usuario.setDescricao(usuarioUpdate.getDescricao());
    sessao.update(usuario);
}

如果我不使用“sessao.flush();”在 DAO 方法结束时......我的对象没有被更新。在 tomcat 中看不到任何更新语句。正如我计划配置它我不需要 .flush() 对吗?!?!?魔法在哪里(笑)?

有什么建议吗?提前致谢。

编辑 1)

我确实使用 org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

我还读到我们必须将“flush_mode”(或类似的东西)设置为 AUTO?这真的需要吗?

编辑 2)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration>

 <session-factory>
   <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
   <property     name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
   <property name="hibernate.connection.url">jdbc:sqlserver://XXX.XX.XX.XX:1433;DatabaseName=WEBCLIENTES
   </property>
   <property name="hibernate.connection.username">XXXXXXX</property>
   <property name="hibernate.connection.password">YYYYYYY</property>
   <property name="hibernate.connection.pool_size">10</property>
   <property name="show_sql">true</property>
   <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>



   <mapping class="br.com.clarkemodet.clientes.model.Usuario" />
   <mapping class="br.com.clarkemodet.clientes.model.Cliente" />
   <mapping class="br.com.clarkemodet.clientes.model.Acesso" />
   <mapping class="br.com.clarkemodet.clientes.model.Historico" />
   <mapping class="br.com.clarkemodet.clientes.model.Inventor" />
   <mapping class="br.com.clarkemodet.clientes.model.NotaDebito" />
   <mapping class="br.com.clarkemodet.clientes.model.Titular" />
   <mapping class="br.com.clarkemodet.clientes.model.Processo" />

  </session-factory>

  </hibernate-configuration>

编辑 3)

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Archetype Created Web Application</display-name>

 <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring Open Session In View Pattern filter -->
<filter>
<filter-name>hibernateFilter</filter-name>
  <filter-  class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>sessionFactory</param-value>
</init-param>
</filter>

<!-- Spring/Hibernate filter mappings -->
 <filter-mapping>
 <filter-name>hibernateFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <session-config>
  <session-timeout>5</session-timeout>
 </session-config>  

 </web-app>

编辑 4)我的 spring-servlet.xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:annotation-config />
<context:component-scan base-package="br.com.clarkemodet.clientes" />

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/resources/**" location="/resources/frontend/" />
<mvc:annotation-driven />

<mvc:interceptors>
    <bean class="br.com.clarkemodet.clientes.interceptors.AutorizadorInterceptor" />
</mvc:interceptors>

</beans>

【问题讨论】:

  • 请添加您的hibernate.cfg.xml。使用 spring 时,flush-mode auto 是默认设置。
  • 我猜你有 2 个服务实例,一个是事务性的,一个不是事务性的。我怀疑当您删除 OpenSessionInViewFilter 时,您会收到一个异常,指出它无法启动事务。确保您使用的服务实例与&lt;tx:annotation-driven /&gt; 处于相同的上下文中,否则不会应用任何事务。
  • 我刚刚发布了我的 hibernate.cfg.xml。但我怎么会有 2 个实例?我使用服务接口,有一个实现,它使用接口自动连接到控制器。设置了服务和事务注释......我如何检查“与 tx:annotation-driven 相同的上下文中的服务”?我不明白。在此先感谢人
  • 您有一个ContextLoaderListener 和一个DispatcherServlet,从您发布的sn-ps 来看,至少您的&lt;tx:annotation-driven /&gt; 是由ContextLoaderListener 加载的。现在,如果您的配置中有一个&lt;context:component-scan /&gt;,它由检测和实例化bean 的ContextLoaderListener 加载。现在您可能在DispatcherServlet 加载的配置中拥有相同的&lt;context:component-scan /&gt;。稍后将创建一个新的 bean 实例,因为您的 &lt;tx:annotation-driven /&gt; 不存在,现在没有更多交易。
  • 我刚刚发布了我的 web.xml 和 spring-servlet.xml,所以我应该删除或添加一些东西到哪个 xml?

标签: spring session flush transactionmanager


【解决方案1】:

生日快乐

  1. 通过 sessao.get 更改 sessao.load
  2. DAO 类中的 updateUsuario 方法也必须是 @Transactional。

【讨论】:

  • 嘿.. 但是如果我的服务层方法有@transactional,那么它内部调用的DAO方法也不是事务性的吗?我会试一试,告诉你这是否有效。提前致谢。
  • 不,如果你想让事务中的DAO从服务启动,它也必须是@Transactional。
  • 按照您的指示执行相同的行为。我编辑了我的帖子,我使用了 hibernate 的 openviewfilter,有人说你需要将 flush 属性设置为 AUTO,因为默认是 NEVER.. 对吗?
  • spring 文档:“注意:默认情况下,此过滤器不会刷新 Hibernate 会话,刷新模式设置为 FlushMode.NEVER。它假定与关心的服务层事务结合使用flushing:活动事务管理器将在读写事务期间临时将刷新模式更改为 FlushMode.AUTO,并在每个事务结束时将刷新模式重置为 FlushMode.NEVER。如果您打算在没有事务的情况下使用此过滤器,请考虑更改默认刷新模式(通过“flushMode”属性)。”.. 我不应该担心它,对吧?
  • 1.是的,但不应该是一个问题。 2. 没有必要,只要在这种情况下有一个从服务启动的事务。
猜你喜欢
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多