0

出于某种原因,除非我明确地刷新(),否则我无法通过休眠来保存我的对象。

我正在使用 Spring MVC

进行保存的 DAO 的一部分

public final T saveOrUpdate(final T instance) {
    context.currentSession().saveOrUpdate(instance);
    context.currentSession().flush(); //TODO should not have to do this
    return instance;
}

web.xml 文件的一部分,允许通过 AJAX 从视图中查询

<filter>
    <filter-name>Open Session In View Filter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>Open Session In View Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

用于事务管理的 spring 配置的一部分

<context:property-placeholder location="classpath:environment.properties" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${hibernate.connection.driver_class}" />
  <property name="jdbcUrl" value="${hibernate.connection.url}" />
  <property name="user" value="${hibernate.connection.username}" />
  <property name="password" value="${hibernate.connection.password}" />
  <property name="initialPoolSize" value="5" />
  <property name="minPoolSize" value="5" />
  <property name="maxPoolSize" value="25" />
  <property name="acquireIncrement" value="5" />
  <property name="maxIdleTime" value="1800" />
  <property name="numHelperThreads" value="5" />
</bean>


<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="entityInterceptor">
    <bean class="org.mycompany.persistence.AuditTrailInterceptor"/>
  </property>
  <property name="hibernateProperties">
    <props>
      <!-- Hibernate Tweak to enhance performance -->
      <prop key="hibernate.order_inserts">true</prop>
      <!-- Hibernate Tweak to enhance performance -->
      <prop key="hibernate.order_updates">true</prop> 

      <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
      <prop key="hibernate.show_sql">true</prop>
    </props>
  </property>
  <property name="configLocation" value="classpath:hibernate.cfg.xml" />

  <!-- Enable mapping of annotated hibernate classes -->
  <property name="packagesToScan" value="org.mycompany" />

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

示例服务方法

@Service
@Transactional
class MyServiceImpl implements MyService {

...

    @Override
    public final void save(final MyObject obj) {
        myObjectDao.save(obj);
    }
4

1 回答 1

0

原来是我的 root-context.xml 和 servlet-context.xml 之间的配置问题,我必须执行以下操作:

我必须将以下内容放在 root-context.xml 中:

<!-- Load everything except @Controllers -->
<context:component-scan base-package="my.package">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

在 servlet-context.xml 中:

<!-- Search this package for annotated Spring Beans -->
<!-- Load @Controllers only -->
<context:component-scan base-package="my.package" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

请注意,这use-default-filters="false"很重要,也是我最初遇到很多麻烦的地方,看来 servlet 正在覆盖 root-context.xml 中的 bean

于 2013-06-27T03:53:49.777 回答