本文介绍了通过org.springframework.orm.hibernate4.LocalSessionFactoryBean创建HibernateTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了以下 sessionFactory

p>

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean> 
< property name =dataSourceref =myDataSource/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.Oracle10gDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto>验证< / prop>
< /道具>
< / property>
< property name =packagesToScanvalue =com.my.company/>
< / bean>

正如你所看到的,上面的 sessionFactory 是对于Hibernate 4.



我试图创建一个 HibernateTemplate org.springframework.orm.hibernate3.HibernateTemplate 通过Java代码,通过 sessionFactory ,但是我我不知道如何做到这一点。



我试过以下代码:

  @Resource(name =sessionFactory)
public void setSessionFactory(LocalSessionFactoryBean sessionFactory){
this.hibernateTemplate = new HibernateTemplate(sessionFactory.getObject());
}

但是我收到以下错误消息:

  org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'sessionFactory'的Bean必须是[org.springframework.orm.hibernate4.LocalSessionFactoryBean]类型,但实际上是类型[org.hibernate.internal.SessionFactoryImpl] 

您可以演示如何执行此操作? Hibernate-Classes-Interfaces-in-Spring-orm-3.1.0.RC1#post_386881rel =noreferrer>本文:

从包的javadoc org.springframework.orm.hibernate4
$ b


I work with Spring 3.1 + Hibernate 4.

I've created the following sessionFactory:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.my.company"/>
</bean>

As you see, the above sessionFactory is for Hibernate 4.

I am trying to crate a HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate) through Java code, by that sessionFactory, but I am not sure how to do this.

I've tried the following code:

@Resource(name="sessionFactory")
public void setSessionFactory(LocalSessionFactoryBean sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory.getObject());
}

But I'm getting the following error message:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'sessionFactory' must be of type [org.springframework.orm.hibernate4.LocalSessionFactoryBean], but was actually of type [org.hibernate.internal.SessionFactoryImpl]

Could you show how to do this?

解决方案

As from this post:

From the javadoc of package org.springframework.orm.hibernate4:

这篇关于通过org.springframework.orm.hibernate4.LocalSessionFactoryBean创建HibernateTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:29