tin*_*tin 2 spring hibernate spring-mvc
我想将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>
Run Code Online (Sandbox Code Playgroud)
正在使用会话的DAO:
<bean id="hibernateUserProfileDAO" class="com.springheatmvn.Dao.impl.hibernate.HibernateUserProfileDAO">
<property name="annotationSessionFactoryBean" ref="annotationSessionFactoryBean"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
在我的hibernateUserProfileDAO中,我想得到像这样的当前会话
public class HibernateUserProfileDAO implements UserProfileDAO {
private AnnotationSessionFactoryBean annotationSessionFactoryBean;
public UserProfile getUserProfile() {
Session session = annotationSessionFactoryBean.getCurrentSession();
....
}
Run Code Online (Sandbox Code Playgroud)
但我发现AnnotationFactoryBean中没有公共的getCurrentSession()方法.我发现只有受保护的getAnnotationSession()方法,但它也在Abstract会话工厂类中.
任何人都可以告诉我哪里出错了?
AnnotationSessionFactoryBean是一个SessionFactory自动生成的工厂(内部的Spring句柄),因此您需要按如下方式使用它:
<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>
Run Code Online (Sandbox Code Playgroud)
.
public class HibernateUserProfileDAO implements UserProfileDAO {
private SessionFactory sf;
...
}
Run Code Online (Sandbox Code Playgroud)
然后你Session通过电话获得sf.getCurrentSession().
| 归档时间: |
|
| 查看次数: |
16618 次 |
| 最近记录: |