2011-07-27 252 views
2

所有我有這樣的實體類JPA /休眠刪除實體

public class Entity { 

    @Id 
    @Column(name="ID") 
    private int id; 

    @OneToMany(mappedBy="flusso", cascade=CascadeType.ALL, orphanRemoval=true) 
    private List<Cron> listCron; 

    //getter and setter Method 
} 

,並在我的DAO類我有這個方法

(...) 
    @Transactional 
    public void removeAll(Entity entity) throws PersistenceDaoException { 
     try { 
      final Class<? extends Object> paramClass = entity.getClass(); 
      String nameClass = paramClass.getSimpleName(); 
      Query q = em.createQuery ("DELETE FROM " + nameClass); 
      int del = q.executeUpdate(); 
      if (del == 1){ 
       System.out.println("risultato:" + del); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
(...) 

我做一個簡單的測試,我有這個問題:

javax.persistence.TransactionRequiredException: Executing an update/delete query 
    at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310) 
    at $Proxy24.executeUpdate(Unknown Source) 
    at it.synclab.fb.jpa.dao.impl.GenericDaoImpl.removeAll(GenericDaoImpl.java:138) 
    at it.synclab.fb.jpa.dao.impl.FlussoDaoImpl.removeAll(FlussoDaoImpl.java:143) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196) 
    at $Proxy22.removeAll(Unknown Source) 
    at it.synclab.fb.jpa.test.FlussoDaoTest.main(FlussoDaoTest.java:30) 

此方法用@Transactional標記,我在哪裏做錯了?

PS:這是我的applicationContext.xml:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="fb-persistence" /> 
    </bean> 

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

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


    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 


    <bean name="entityDaoImpl" class="it.synclab.fb.jpa.dao.impl.EntityDaoImpl" /> 
+0

從您的堆棧跟蹤情況來看,它看起來你使用springframework的等。你能提供你的spring context xml嗎? –

回答

0

確保你在你的背景有一個事務管理器。

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

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

http://static.springsource.org/spring/docs/2.5.x/reference/transaction.html(9.5.6)

+0

我添加了我的上下文,你可以看到我有一個事務管理器(「transactionManager」) – giulius