不活跃的transaciotn:javax.persistence.TransactionRequiredException:执行更新/删除查询

时间:2014-06-04 20:24:34

标签: java spring hibernate jpa transactions

为什么我的交易不活跃? 我有这样的信息: " 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)

2 个答案:

答案 0 :(得分:9)

我的xml配置中没有<tx:annotation-driven transaction-manager="transactionManager"/>

答案 1 :(得分:4)

此错误的另一个可能原因。确保使用正确的注释。

在Spring中,使用。

org.springframework.transaction.annotation.Transactional

不是

javax.transaction.Transactional。

这在单元测试中有无数次。