2011-07-12 16 views
0

との取引にはsame problemがあります。トランザクションはDBに書き込まれません。 (理解のためのapplicationContext.xmlを)私はこの春の問題については、フレームワークのバグではありませんが、設定ファイルの問題だと思うので、私は私の設定ファイルを置く:Spring e Jpa

...  
      <!-- this is a bridge for entityManager and PersistenceUnit --> 
     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
      <property name="persistenceUnitName" value="fb-persistence" /> 
     </bean> 


    <bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
    <bean name="pluginDaoImpl" class="it.synclab.fb.jpa.dao.impl.PluginDaoImpl" /> 
</beans> 

インタフェースは次のとおりです。

public interface PluginDao { 

    public Plugin load (int id); 

    public void save(Plugin plg); 
} 

public class PluginDaoImpl implements PluginDao { 

    @PersistenceContext (unitName="fb-persistence") 
    private EntityManager em; 

    public void setEntityManager(EntityManager entityManager) { 
     this.em = entityManager; 
    } 

    @Transactional 
    public Plugin load(int id) { 
     return em.find(Plugin.class, id); 

    } 
    @Transactional(readOnly=false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) 
    public void save(Plugin plg) { 
     em.persist(plg); 
     //em.flush(); 

    } 
} 

とフンドでdulcis、(汚れたテスト用)私のPluginTestである:

インターフェースの実装である

public class PluginTest{ 

    public static void main(String[] args) {   
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     PluginDao dao = (PluginDao) applicationContext.getBean("pluginDaoImpl"); 

     Plugin plugin1 = new Plugin(); 
     //inserisco un nuovo plugin 
     //plugin.setId(14); 
     plugin1.setMethod("prova"); 
     plugin1.setDescrizione("questa Bean Class!"); 
     dao.save(plugin1); 

     Plugin plugin = new Plugin(); 
     //carico un plugin per id 
     plugin = dao.load(9); 

     System.out.println("id: " + plugin.getId() + 
         " Descrizione: " + plugin.getDescrizione() + 
         " Method: " + plugin.getMethod()); 
    } 

私は今、私はこのエラーを取得する方法PluginDaoImpl.save()、em.flush行のコードを追加した場合:原因がspecificatedされていません*

Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress * 

答えて

1

あなたのcontext.xmlに

<tx:annotation-driven transaction-manager="myTransactionManager"/>
が欠落している(Using @Transactionalを参照)

このようなTXの名前空間を追加します。

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
+0

ライン18クラスパスリソースからXML文書内の[ applicationContext.xml]が無効です。ネストされた例外はorg.xml.sax.SAXParseExceptionです。要素 "tx:annotation-driven"の接頭辞 "tx"はバインドされていません。 – giulius

+0

@giulius次に、tx名前空間も追加する必要があります。私の更新された回答を参照してください –

+0

あなたは春を書くために協力しています:P..thanks、それは今、dbに "持続"しています。完全性のために、あなたのリンクを読んで、Springについて学んでください。 ;) – giulius