hibernate4.LocalSessionFactoryBean无法强制转换为org.hibernate.cfg.Configuration

时间:2012-05-30 14:19:36

标签: java spring hibernate

这似乎是一个常见的问题,但我无法解决它。 我的Spring 3.1.1配置是

<bean id="sessionFactoryEditSolution" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="editSolutions-pool"/>
        <property name="mappingResources">
            <list>
                <value>/editsolutions.hibernate.cfg.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
                <prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
                <prop key="hibernate.max_fetch_depth">6</prop>
                <prop key="hibernate.default_schema">dbo</prop>
            </props>
        </property>
    </bean> 

这就是我试图获取配置对象的方法

Configuration editSolutionsConfiguration = `(Configuration)AppContext.getBean("&sessionFactoryEditSolution");`

最初我的 Hibernate 3 用于我的应用程序但是要将它与Spring 3.1.1集成我已将其升级到 Hibernate 4 。这就是我必须保留hibernate3的原因。我的lib文件夹中的jar支持几个hibernate 3特定的代码行。

1 个答案:

答案 0 :(得分:1)

几点说明:

  • 你不能在类路径上使用两个版本的hibernate。您的旧代码应该更新
  • &x返回工厂bean,它不返回生成的对象。即使没有&符号,也会返回SessionFactory,而不是Configuration
  • 春天你实际上不需要Configuration,它在幕后处理
  • 我们正在使用hibernate 3和spring 3.1。 3.1.1可能不同,但要么保留两者的较低版本,要么同时升级(包括代码)