gpt4 book ai didi

java - JPA/Hibernate 删除实体中的所有内容

转载 作者:行者123 更新时间:2023-11-29 09:20:27 25 4
gpt4 key购买 nike

我有这个实体类

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" />

最佳答案

确保您的上下文中有事务管理器。

<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)

关于java - JPA/Hibernate 删除实体中的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com