Spring中的Hibernate事务管理器配置 [英] Hibernate transaction manager configurations in Spring

查看:413
本文介绍了Spring中的Hibernate事务管理器配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用Hibernate和编程事务划分。
每次在我的Service方法中,我都写了类似的东西。

  Session session = HibernateUtil.getSessionFactory()。的openSession(); 
session.beginTransaction();
..在这里执行操作
session.getTransaction()。commit();

现在我要用声明式事务管理重构我的代码。现在我得到了......

 < context:component-scan base-package =package。*/> 

< bean id =mySessionFactoryclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =configLocationvalue =classpath:hibernate.cfg.xml>< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< / bean>

< tx:annotation-driven transaction-manager =txManager/>


< bean id =txManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactory>
< ref local =mySessionFactory/>
< / property>
< / bean>

服务等级:

  @Service 
public class TransactionalService {

@Autowired
Private SessionFactory factory;

@Transactional
public User performSimpleOperation(){
return(User)factory.getCurrentSession()。load(User.class,1L);
}
}



&简单测试 -


  @Test 
public void useDeclarativeDem(){
FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(spring-config.xml);
TransactionalService b =(TransactionalService)ctx.getBean(transactionalService);
User op = b.performSimpleOperation();
op.getEmail();

当我尝试在事务性方法以外获取用户电子邮件时,我得到了懒惰初始化异常,电子邮件是我的情况是一个简单的字符串。 Hibernate甚至不执行sql查询,直到我调用POJO上的任何getter。

<1>我在这里做错了什么?

<2>这种方法是否有效?



3)你可以推荐任何opensource项目,在spring / hibernate上使用基于注解的配置吗?
$ b

更新



由于某些原因,如果我将 getCurrentSession 替换为 openSession ,此代码可以正常工作。有人可以解释一下吗?



谢谢

解决方案

最后我意识到了什么是问题。在上面的代码中,我使用load来代替get。 Session.load实际上并没有打到数据库。这就是为什么我在@Transactional方法之外获得懒惰初始化异常的原因。如果我使用openSession而不是getCurrentSession,会话将在范围spring容器之外打开。由于结果会话并不紧密,它允许我读取@Transactional方法以外的对象属性。

In my project I use Hibernate with programmatic transaction demarcation. Every time in my Service methods i write something similar to this.

Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
.. perform operation here
session.getTransaction().commit();

Now i'm going to refactor my code with declarative transaction management. What i got now ...

  <context:component-scan base-package="package.*"/>

  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
  </bean>

  <tx:annotation-driven transaction-manager="txManager"/>


  <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
         <ref local="mySessionFactory"/>
    </property>
  </bean>

Service class:

@Service
public class TransactionalService {

    @Autowired
    private SessionFactory factory;

    @Transactional
    public User performSimpleOperation() {
        return (User)factory.getCurrentSession().load(User.class, 1L);
    }
}

And simple test -

@Test
public void useDeclarativeDem() {
    FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("spring-config.xml");
    TransactionalService b = (TransactionalService)ctx.getBean("transactionalService");
     User op = b.performSimpleOperation();
     op.getEmail();

When i try to get user email outside of Transactional method, i got Lazy initialization exception, email is my case is a simple string. Hibernate does not even perform sql query, until i call any getters on my POJO.

1) what i am doing wrong here ?

2) Is this approach valid ?

3) Can you suggest any opensources project wich work on spring/hibernate with annotation based configuration ?

Update

For some reason if i replace getCurrentSession with openSession this code works fine. Can someone explain it please ?

Thank you

解决方案

Ok, finally i realized what was the problem. In code above i used load instead of get. Session.load did not actually hit the databased. That's the reason why i get lazy-initialization exception outside of @Transactional method

If i use openSession instead of getCurrentSession, session is opened outside of scope spring container. As result session was not close and it allow me to read object properties outside of @Transactional method

这篇关于Spring中的Hibernate事务管理器配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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