HibernateTransactionManager + Spring + update()=会话不刷新

时间:2014-07-09 19:12:48

标签: spring session flush transactionmanager

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

在我的applicationcontext.xml中,我得到了sessionfactory和transactionManager(都使用默认名称)set。

<?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);
}

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

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

编辑1)

我使用的是org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

我还读过我们必须将&#34; flush_mode&#34;(或类似的东西)设置为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>

1 个答案:

答案 0 :(得分:0)

Bom dia

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