为什么我的交易不活跃?
我有这样的信息:
“ javax.persistence.TransactionrequiredException:执行更新/删除查询”
我找不到原因
我有这样的信息:
“ javax.persistence.TransactionrequiredException:执行更新/删除查询”
我找不到原因
aplicationContext:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <context:annotation-config /> <context:component-scan base-package="com.model.dao" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.MysqL.jdbc.Driver" /> <property name="url" value="jdbc:MysqL://localhost:3306/punit" /> <property name="username" value="root" /> <property name="password" value="" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="10" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="org.hibernate.dialect.MysqLDialect" /> </bean> </property> <property name="jpaPropertyMap"> <map> <entry key="hibernate.connection.useUnicode" value="true" /> <entry key="hibernate.connection.characterEncoding" value="UTF-8" /> <entry key="hibernate.jdbc.batch_size" value="30" /> </map> </property> </bean> <!-- Transaction Manager --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <!-- <property name="dataSource" ref="dataSource" /> --> </bean>
DAO Impl
@Component public class UserDaoImpl implements UserDao { @PersistenceContext(unitName="punit") private EntityManager em; protected UserDaoImpl() { } //other ovverrides like find,get @Override @Transactional public void deleteUser(String mail,String password) { Query q = em.createQuery("DELETE FROM User u WHERE u.mail='" + mail + "'AND u.password='" + password + "'"); try{ q.executeUpdate(); this.em.flush(); }catch (PersistenceException pe){ pe.printStackTrace(); } }
}
persistence.xml中
<?xml version="1.0"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit transaction-type="RESOURCE_LOCAL" name="punit"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <!-- e.g. validate | update | create | create-drop --> </properties> </persistence-unit> </persistence>
RemoveUser
public class RemoveUser extends BaseAction{ private static final long serialVersionUID = 1L; private String mail; private String password; User user; @Autowired UserDaoImpl u; @Action(value = "removeUser",results = { @Result(name="success",location = "pages/loginform.jsp"),@Result(name="login",location = "pages/loginform.jsp")}) public String removeUser(){ u.deleteUser(mail,password); //..logoutAction return SUCCESS; } //..getters and setters... }
javax.persistence.TransactionrequiredException: Executing an update/delete query at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96) at com.model.dao.UserDaoImpl.deleteUser(UserDaoImpl.java:73) at com.actions.RemoveUser.removeUser(RemoveUser.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
解决方法
我没有看到< tx:annotation-driven transaction-manager =“transactionManager”/>在您的xml配置中.