I have this Entity Class
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
}
and in my Dao Class i have this method
(...)
@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();
}
(...)
I do a simple test and i have this problem:
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)
This method is marked with @Transactional, where am I doing wrong?
PS:This is my 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" />
Make sure you have a transaction manager in your context.
<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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.