1

我正在尝试测试我的应用程序的乐观锁定实现。然而结果不是我所期望的。我测试的步骤如下

  • 从数据库中加载实体
  • 将版本属性设置为比数据库中存在的值小一
  • 将另一个只是字符串的属性更改为其他属性
  • 保存实体

我现在预计会出现 staleException,但是实体刚刚被保存并且版本增加到下一个。

这是我保存代码的一小部分

   public <T extends DomainObject> T save(T objectToSave) {
        Session currentSession = null;
        try {
            currentSession = sessionFactory.getCurrentSession();
            currentSession.save(objectToSave);
            return objectToSave;
        } catch (Exception ex) {
            logger.error("save error",ex);
        }
        return null;
    }

我使用以下代码通过我的整个应用程序通过命名查询按 id 加载对象

@SuppressWarnings("unchecked")
public <T extends Object> List<T> query(Class<T> returnClass, String query, List<String> namedParams, List<? extends Object> params, Integer limit) {
    Session currentSession = null;
    try {
        currentSession = sessionFactory.getCurrentSession();
        Query namedQuery = currentSession.getNamedQuery(query);
        if(limit != null){
            namedQuery.setMaxResults(limit);
        }
        namedQuery.setCacheable(true);
        if (namedParams != null && namedParams.size() > 0) {
            addParams(namedQuery, namedParams, (List<Object>) params);
        }
        return namedQuery.list();
    } catch (Exception ex) {
        logger.error("query error",ex);
    }
    return null;
}

这是我的 sessionfactory 的配置

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="entityInterceptor" ref="auditInterceptor" />
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=${hibernate.dialect}
            hibernate.show_sql=${hibernate.show_sql}
            hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
            hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
            hibernate.cache.use_query_cache=true
            hibernate.cache.use_second_level_cache=true
            hibernate.cache.provider_configuration_file_resource_path=ehcache.xml
            hibernate.generate_statistics=true
            hbm2ddl.auto=${hbm2ddl.auto}
            hibernate.c3p0.min_size=${hibernate.c3p0.min_size}
            hibernate.c3p0.max_size=${hibernate.c3p0.max_size}
            hibernate.c3p0.timeout=${hibernate.c3p0.timeout}
            hibernate.c3p0.max_statements=${hibernate.c3p0.max_statements}
            hibernate.c3p0.idle_test_period=${hibernate.c3p0.idle_test_period}
        </value>
    </property>
    <property name="schemaUpdate">
        <value>true</value>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.mbalogos.mba.domain.site.Site</value>
        </list>
    </property>
</bean>          

我在 sessionFactory 的配置上遗漏了什么,还是我测试它完全错误,我应该以不同的方式测试它吗?

提前致谢

4

1 回答 1

1

Regressing the version field is dangerous as it is Hibernate controlled (if you're using the JPA versioning functionality). Load in an entity, detach it (maybe use session.evict(obj)), alter an attribute. Load in the same entity, keep it attached, alter it, save it. Reattach the first entity and attempt to save it (I think Merge will do this). You should then see the StaleObjectStateException exception.

于 2012-10-24T08:40:34.123 回答