如何通过AnnotationSessionFactoryBean访问休眠会话? [英] How to access hibernate session via AnnotationSessionFactoryBean?

查看:66
本文介绍了如何通过AnnotationSessionFactoryBean访问休眠会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将休眠与Spring集成在一起. Spring 3文档说,您可以通过org.hiberate.SessionFactory的getCurrentSession()访​​问会话,并且它比hibernateDaoSupport方法更受欢迎.

I want to integrate hibernate with spring. spring 3 documentation says that you can access session via org.hiberate.SessionFactory's getCurrentSession() and this should be prefered over hibernateDaoSupport approach.

但是我想知道如果我们正在使用AnnotationSessionFactoryBean的话,如何首先获得org.hiberate.SessionFactory的实例? 我在applicationContext.xml中完成了以下bean声明:

But I want to know how can we get hold of org.hiberate.SessionFactory's instance in the first place if we are using AnnotationSessionFactoryBean? I have done the following bean declaration in applicationContext.xml:

       <bean id="annotationSessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <property name="packagesToScan" value="com.mydomain"/>
              <property name="hibernateProperties">
                  <props>
                    <prop key="hibernate.connection.pool_size">10</prop>
                    <prop key="hibernate.connection.show_sql">true</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                  </props>
            </property>     
    </bean>

正在使用会话的DAO:

DAO which is using the session:

    <bean id="hibernateUserProfileDAO" class="com.springheatmvn.Dao.impl.hibernate.HibernateUserProfileDAO">
        <property name="annotationSessionFactoryBean" ref="annotationSessionFactoryBean"/>
    </bean>

在我的hibernateUserProfileDAO中,我想要这样的当前会话

In my hibernateUserProfileDAO I would like to get the current session like this

    public class HibernateUserProfileDAO implements UserProfileDAO {
      private AnnotationSessionFactoryBean annotationSessionFactoryBean;

      public UserProfile getUserProfile() {
    Session session = annotationSessionFactoryBean.getCurrentSession();
      ....
      }

但是我看到AnnotationFactoryBean中没有公共的getCurrentSession()方法.我发现只有受保护的getAnnotationSession()方法,但它也位于Abstract session factory类上.

But I see that there is no public getCurrentSession() method in AnnotationFactoryBean. I found only protected getAnnotationSession() method but it is also on Abstract session factory class.

谁能告诉我我要去哪里错了?

Can any one please tell me where am i going wrong?

推荐答案

AnnotationSessionFactoryBean是自动生成SessionFactory的工厂(Spring内部处理),因此您需要按以下方式使用它:

AnnotationSessionFactoryBean is a factory that produces SessionFactory automatically (Spring handles in internally), so that you need to use it as follows:

<bean id="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">        
    ...
</bean>    

<bean id="hibernateUserProfileDAO" class="com.springheatmvn.Dao.impl.hibernate.HibernateUserProfileDAO">
     <property name="sf" ref="sf"/>
</bean>

.

public class HibernateUserProfileDAO implements UserProfileDAO {
    private SessionFactory sf;    
    ...
}

然后您通过调用sf.getCurrentSession()获得Session.

这篇关于如何通过AnnotationSessionFactoryBean访问休眠会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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