0

私は、JSF、Spring、および Hibernate フレームワークを持つアプリケーションに取り組んでいます。次の方法を使用して、Spring のトランザクションをテストしようとしています。

@Override
@Transactional(rollbackFor=UnsupportedOperationException.class, propagation=Propagation.REQUIRED)
public void updateActiveMonth(Long collectionMonthId) throws Exception{
    try{
    Session session = getSessionFactory().getCurrentSession();
    String hql = "update CollectionMonth collectionMonth set active = false";
    Query query = session.createQuery(hql);
    query.executeUpdate();
    if (true) {
        throw new UnsupportedOperationException();
    }
    String hql1 = "update CollectionMonth collectionMonth set collectionMonth.active = true where collectionMonth.id=:collectionMonthId";
    Query query1 = session.createQuery(hql1);
    query1.setLong("collectionMonthId", collectionMonthId);
    query1.executeUpdate();
    }catch(UnsupportedOperationException e){
        throw e;
    }
}

上記の方法は、最初のクエリによって行われた更新をロールバックすることを想定していますが、これは起こりません。

アプリケーションのコンテキストでは、次の設定があります

<context:annotation-config/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"/>


<!-- PERSISTENCE SUPPORT HERE (Hibernate 4 Mapping In Spring ) -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:configLocation="/WEB-INF/hibernate.cfg.xml" 
      p:packagesToScan="collection.model"/>

    <!-- Transaction Manager is defined -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory" />

<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

誰が私が間違っているのか教えてもらえますか?

4

1 に答える 1

0

クラスをSpring Beanとして宣言するのを忘れたので、Springコンテナーはそれらを検出して登録できます。あなたのメソッドpublic void updateActiveMonth(Long collectionMonthId)は、春のためにどこかで宣言されているある種のサービスまたはdaoクラスの一部でなければなりません。

これらを xml で宣言したり、annotation-config と component-scan を使用して適切な注釈を設定したりするなど、これを行うにはいくつかの方法があります。

この素晴らしい記事を最初から最後まで読むことをお勧めします。

于 2013-05-31T12:14:31.600 に答える