简体   繁体   English

与Hibernate4联合

[英]Unitils with Hibernate4

I'm trying to upgrade an Struts/Spring application from Hibernate 3.1 to Hibernate 4.3. 我正在尝试将Struts / Spring应用程序从Hibernate 3.1升级到Hibernate 4.3。

I'm using Untils for my database tests, and under Hibernate 3.1, the tests were working correctly. 我在数据库测试中使用的是直至,并且在Hibernate 3.1下,测试正常工作。 Under Hibernate 4.3, the tests are failing with the following error: 在Hibernate 4.3下,测试失败并显示以下错误:

org.unitils.core.UnitilsException: Could not find a configuring @HibernateSessionFactory annotation or custom config method
    at org.unitils.orm.common.OrmModule.getConfiguredPersistenceUnit(OrmModule.java:186)
    at org.unitils.orm.common.OrmModule.getPersistenceUnit(OrmModule.java:147)
    at org.unitils.orm.common.OrmModule.injectOrmPersistenceUnitIntoTestObject(OrmModule.java:333)
    at org.unitils.orm.common.OrmModule$OrmTestListener.beforeTestSetUp(OrmModule.java:379)
    at org.unitils.core.Unitils$UnitilsTestListener.beforeTestSetUp(Unitils.java:273)
    at org.unitils.UnitilsJUnit4TestClassRunner$TestListenerInvokingMethodRoadie.runBeforesThenTestThenAfters(UnitilsJUnit4TestClassRunner.java:181)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.unitils.UnitilsJUnit4TestClassRunner.invokeTestMethod(UnitilsJUnit4TestClassRunner.java:95)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
    at org.unitils.UnitilsJUnit4TestClassRunner.access$000(UnitilsJUnit4TestClassRunner.java:42)
    at org.unitils.UnitilsJUnit4TestClassRunner$1.run(UnitilsJUnit4TestClassRunner.java:60)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
    at org.unitils.UnitilsJUnit4TestClassRunner.run(UnitilsJUnit4TestClassRunner.java:67)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

For example, the following test will fail: 例如,以下测试将失败:

@SpringApplicationContext({ configurationFiles })
public class SettlementDAOImplTest extends UnitilsJUnit4 {
    @HibernateSessionFactory
    private SessionFactory sessionFactory;

    @Test
    public void type() throws Exception {
        assertThat(SettlementDAOImpl.class, notNullValue());
    }
}

I've checked my libraries, and I'm loading the correct versions of Unitils and Hibernate, and my sessionFactory is defined as a org.springframework.orm.hibernate4.LocalSessionFactoryBean 我检查了我的库,并加载了正确版本的Unitils和Hibernate,并将sessionFactory定义为org.springframework.orm.hibernate4.LocalSessionFactoryBean

At this point, I'm not even sure how to proceed. 在这一点上,我什至不知道如何进行。 It appears that Unitils is looking for a hibernate3.localSessionFactoryBean. 看来Unitils正在寻找hibernate3.localSessionFactoryBean。 What should I be looking for to troubleshoot this problem? 我应该寻找什么来解决此问题?

In my applicationcontext file, the sessionFactory looks like: 在我的applicationcontext文件中,sessionFactory看起来像:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref local="dataSource" />
    </property>

    <property name="mappingResources">
        <list>
            <value>Settlement.hbm.xml</value>
        </list>
    </property>


    <property name="hibernateProperties">
        <props>
            <!-- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> -->
            <prop key="hibernate.dialect">
                com.acteva.commons.util.MySQLDialect
            </prop>
            <prop key="hibernate.bytecode.provider">javassist</prop>
            <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.connection.release_mode">
                auto
            </prop>
        </props>
    </property>

    <property name="entityInterceptor" ref="entityInterceptorBean" />
</bean>

This may be related to explanation below: 这可能与以下说明有关:

Source: http://www.unitils.org/tutorial-hibernate.html 资料来源: http : //www.unitils.org/tutorial-hibernate.html

Under the hoods, hibernate uses an instance of org.hibernate.cfg.Configuration or org.hibernate.cfg.AnnotationConfiguration for loading configuration files and registering mapped classes, depending on whether you're using hibernate the classic way or whether you use hibernate with annotation. 在后台,hibernate使用org.hibernate.cfg.Configuration或org.hibernate.cfg.AnnotationConfiguration的实例来加载配置文件和注册映射的类,这取决于您使用的是经典方式的hibernate还是将hibernate与注解。 By default, Unitils uses org.hibernate.cfg.AnnotationConfiguration. 默认情况下,Unitils使用org.hibernate.cfg.AnnotationConfiguration。 If you're using mapping files only and you don't have the hibernate annotations extension in your classpath, you can change this by setting following property: 如果仅使用映射文件,并且在类路径中没有休眠注释扩展名,则可以通过设置以下属性来进行更改:

HibernateModule.configuration.implClassName=org.hibernate.cfg.Configuration HibernateModule.configuration.implClassName = org.hibernate.cfg.Configuration

@Ozan Tabak : We are using the following property, and we encounter the same issue. @Ozan Tabak:我们正在使用以下属性,并且遇到相同的问题。

HibernateModule.configuration.implClassName=org.hibernate.cfg.Configuration

It looks like it's a bug in unitils, which doesn't seem to support hibernate4 看来这是unitils中的错误, 似乎不支持hibernate4

Unitils ver 3.3 does not support loading sessionFactory by @SpringApplicationContext when sessionFactory is defined by org.springframework.orm.hibernate4.LocalSessionFactoryBean. 当由org.springframework.orm.hibernate4.LocalSessionFactoryBean定义sessionFactory时,Unitils版本3.3不支持通过@SpringApplicationContext加载sessionFactory。

Unitils searches for "org.springframework.orm.hibernate3.LocalSessionFactoryBean" and can't find the class because for hibernate 4 there is new namespace "org.springframework.orm.hibernate4.LocalSessionFactoryBean". Unitils搜索“ org.springframework.orm.hibernate3.LocalSessionFactoryBean”,但找不到该类,因为对于休眠4,有一个新的命名空间“ org.springframework.orm.hibernate4.LocalSessionFactoryBean”。

Since this issue is there since September 2012, I don't expect a bug fix soon. 由于自2012年9月以来就存在此问题,因此我预计不会很快修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM