通过org.springframework.orm.hibernate4.LocalSessionFactoryBean创建HibernateTemplate [英] Creating HibernateTemplate by org.springframework.orm.hibernate4.LocalSessionFactoryBean

查看:888
本文介绍了通过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>本文:


不推荐再使用HibernateTemplate(关于自hibernate 3.0.1的
版本以来),没有更多的附加值,因此不再有
会在那里hibernate 4.只需使用纯
SessionFactory并使用getCurrentSession方法获取
curre nt事务会话(不要使用openSession !!!!),你是
好​​去...


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


包含Spring的事务SPI的实现本地
Hibernate事务。这个包有意地是最小的,
没有模板类或类似的东西,为了尽可能地遵循原生的
Hibernate建议。



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:

HibernateTemplate isn't recommended for use anymore (about since the release of hibernate 3.0.1) there is no more added value so that isn't going to be in there anymore for hibernate 4. Simply use the plain SessionFactory and use the getCurrentSession method to obtain the current transactional session (don't use openSession!!!!) and you are good to go...

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

Contains an implementation of Spring's transaction SPI for local Hibernate transactions. This package is intentionally rather minimal, with no template classes or the like, in order to follow native Hibernate recommendations as closely as possible.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆