【发布时间】:2011-09-12 14:10:24
【问题描述】:
我想把hibernate和spring结合起来。 spring 3 文档说您可以通过 org.hiberate.SessionFactory 的 getCurrentSession() 访问会话,这应该优于 hibernateDaoSupport 方法。
但是我想知道,如果我们使用 AnnotationSessionFactoryBean,我们首先如何获取 org.hiberate.SessionFactory 的实例? 我在 applicationContext.xml 中做了以下 bean 声明:
<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:
<bean id="hibernateUserProfileDAO" class="com.springheatmvn.Dao.impl.hibernate.HibernateUserProfileDAO">
<property name="annotationSessionFactoryBean" ref="annotationSessionFactoryBean"/>
</bean>
在我的 hibernateUserProfileDAO 中,我想像这样获取当前会话
public class HibernateUserProfileDAO implements UserProfileDAO {
private AnnotationSessionFactoryBean annotationSessionFactoryBean;
public UserProfile getUserProfile() {
Session session = annotationSessionFactoryBean.getCurrentSession();
....
}
但我看到 AnnotationFactoryBean 中没有公共的 getCurrentSession() 方法。我发现只有受保护的 getAnnotationSession() 方法,但它也在抽象会话工厂类中。
谁能告诉我哪里出错了?
【问题讨论】:
标签: hibernate spring spring-mvc