Hibernate 会话的 Spring 配置
你好 我试图通过Spring的注入来获取hibernate的Session。
这是我的 spring 上下文 xml:
<!-- hibernate's session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:./hibernate.cfg.xml</value>
</property>
</bean>
<!-- the transaction manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
这是代码:
private static ApplicationContext ctx;
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext("springContext.xml");
}
LocalSessionFactoryBean sf = ctx.getBean(LocalSessionFactoryBean.class);
session = sf.getObject().getCurrentSession();
但是我获得的会话为空。
通过 sf.getObject().getCurrentSession() 获取 Session 是否正确?
谢谢 :)
Hi
I'm trying to obtain hibernate's Session through Spring's injection.
Here's my spring context xml:
<!-- hibernate's session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:./hibernate.cfg.xml</value>
</property>
</bean>
<!-- the transaction manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Here's the code:
private static ApplicationContext ctx;
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext("springContext.xml");
}
LocalSessionFactoryBean sf = ctx.getBean(LocalSessionFactoryBean.class);
session = sf.getObject().getCurrentSession();
However the session I obtain is null.
Is it correct to get Session through sf.getObject().getCurrentSession() ?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不应该这样访问会话。使用
HibernateTemplate
或在您的 bean 中注入SessionFactory
并对其调用getCurrentSession()
。否则你的交易管理将无法得到正确处理I don't think you should access the session like that. Either use
HibernateTemplate
or injectSessionFactory
in your beans and callgetCurrentSession()
on it. Otherwise your transaction management won't be handled properly