2010-03-01 98 views
1

我試圖做與@Transactional春季交易沒有任何成功。從我的applicationContext.xml@Transactional不能與春天和冬眠

摘錄:

<context:annotation-config /> 
<context:component-scan base-package="hibex" /> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="org.postgresql.Driver" p:url="jdbc:postgresql://localhost:5432/hibex" 
    p:username="clubspace" p:password="clubspace" /> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation" value="classpath:/hibernate.cfg.xml" /> 
    <property name="entityInterceptor" ref="beanValidationEventListener" /> 
</bean> 

<!-- a PlatformTransactionManager is still required --> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <!-- org.springframework.transaction.jta.JtaTransactionManager 
    org.springframework.jdbc.datasource.DataSourceTransactionManager --> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<tx:annotation-driven /> 

<aop:aspectj-autoproxy /> 

我充滿春天配置文件是here

而且我的方法,如果交易不工作:

@Transactional 
public void m2() { 
    OwnerBO owner2 = ownerManager.get(owner.getId()); 
    owner2.getPets().add(new PetBO("Ubul")); 
} 

這會導致:

1375 [main] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: hibex.bo.OwnerBO.pets, no session or session was closed 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: hibex.bo.OwnerBO.pets, no session or session was closed 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) 
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) 
    at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212) 
    at hibex.code.Service.m2(Service.java:52) 
    at hibex.code.App.run(App.java:15) 
    at hibex.code.Main.main(Main.java:14) 
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: hibex.bo.OwnerBO.pets, no session or session was closed 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) 
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) 
    at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212) 
    at hibex.code.Service.m2(Service.java:52) 
    at hibex.code.App.run(App.java:15) 
    at hibex.code.Main.main(Main.java:14) 

的服務是從這個類叫做:

@Component 
public class App { 
    @Autowired 
    private Service service; 
    public void setService(Service service) { 
     this.service = service; 
    } 
    public void run() { 
     service.m2(); 
    } 
} 

任何想法?

GenericManager是在這裏:GenericManager on Pastebin

get方法:

public T get(PK id) { 
    return dao.findById(id); 
} 

而且GenericDaoImpl#findById(PK)

public E findById(PK id) { 
    return getHibernateTemplate().get(getEntityClass(), id); 
} 

感謝

更新日誌:只是增加了額外的相關信息(必需的代碼段)

+0

你能告訴我OwnerManager.get()的代碼(我懷疑這是在你的通用模板中)。問題與交易沒有太大的關係,但事實上Hibernate Session會在你不想要的時候關閉 – 2010-03-01 11:02:52

+0

我修改後顯示get() – pihentagy 2010-03-01 12:45:04

回答

0

這個異常與Hibernate延遲加載支持有關。 當您調用owner2.getPets()。add()時,看起來Hibernate會話未打開。 難道是ownerManager.get()正在關閉Session嗎?

+0

我修改了帖子以包含'get()'方法 – pihentagy 2010-03-01 12:45:42

+0

「延遲加載也只適用於一個開放的Hibernate會話,無論是在事務中還是在OpenSessionInViewFilter/Interceptor中。」 請參閱http://static.springsource.org/spring/docs/1.2.9/api/org/springframework /orm/hibernate3/HibernateTemplate.html – 2010-03-02 16:39:28

+0

如果尚未註釋OwnerManagerManager#get方法和@Transactional註釋,您可能需要進行註釋。 – 2010-03-02 16:43:55

2

HibernateTransactionManager應提供SessionFactory,請參閱javadoc

+0

那麼你是否說我應該在我的'transactionManager' bean中擁有''? – pihentagy 2010-03-01 16:03:10

+0

@pihentagy:是的 – axtavt 2010-03-01 16:22:16

+0

啊謝謝,這幫了我(upvote),但不是我的問題_final_解決方案 – pihentagy 2010-03-01 17:30:09