简体   繁体   中英

Unitils with Hibernate4

I'm trying to upgrade an Struts/Spring application from Hibernate 3.1 to Hibernate 4.3.

I'm using Untils for my database tests, and under Hibernate 3.1, the tests were working correctly. Under Hibernate 4.3, the tests are failing with the following error:

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

At this point, I'm not even sure how to proceed. It appears that Unitils is looking for a hibernate3.localSessionFactoryBean. What should I be looking for to troubleshoot this problem?

In my applicationcontext file, the sessionFactory looks like:

<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

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. By default, Unitils uses 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

@Ozan Tabak : We are using the following property, and we encounter the same issue.

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

It looks like it's a bug in unitils, which doesn't seem to support hibernate4

Unitils ver 3.3 does not support loading sessionFactory by @SpringApplicationContext when sessionFactory is defined by org.springframework.orm.hibernate4.LocalSessionFactoryBean.

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".

Since this issue is there since September 2012, I don't expect a bug fix soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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