HIBERNATE:TransactionRequiredException:在entityManager.createNativeQuery()之后执行更新/删除查询

时间:2015-03-23 10:12:43

标签: java spring hibernate jpa struts

我正在使用Struts,Spring和Hibernate开发一个项目。单击按钮后,Struts操作引导我完成DS和DAO,在其中我尝试调用本机查询来更新数据库。这是错误跟踪:

  

javax.persistence.TransactionRequiredException:执行更新/删除查询

     

at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:48)

     

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

     

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)

     

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

     

at java.lang.reflect.Method.invoke(Method.java:618)

     

at org.springframework.orm.jpa.SharedEntityManagerCreator $ DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310)

     

$ Proxy496.executeUpdate(未知来源)

     

at com.my.project.MyDAO.unsubscribe(MyDAO.java:307)

     

在com.my.project.MyDS.unsubscribe(MyDS.java:507)

     

在com.my.project.MyDS.updateValue(MyDS.java:784)

     

在com.my.project.MyAction.handleUpdate(MyAction.java:235)

MyDAO:

public class MyDAO extends
    AbstractDAOGenericImpl<MyEntity> {

    /** Entity Manager */
   @PersistenceContext(unitName = "myPersistenceUnit")
   private EntityManager entityManager;

    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int unsubscribe(String entityId) throws JrafDaoException {
        StringBuilder strQuery = new StringBuilder();
        strQuery.append("update MYENTITY set SUBSCRIBE=:subscribe where ENTITY_ID=:entity_id");

        final Query myQuery = getEntityManager().createNativeQuery(strQuery.toString(), MyEntity.class);
        myQuery.setParameter("subscribe", "N");
        myQuery.setParameter("entity_id", entityId);

        try {
            return myQuery.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}

MyDS:

@Autowired
@Qualifier("MyDAO")
private MyDAO myDao;

@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public int unsubscribe(String entityId) throws JrafDomainException {
    return myDao.unsubscribe(entityId);
}

MyAction:

public class MyAction extends DispatchAction {

    private MyDS myDS;

    // ...

    private ActionForward handleUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MyForm myForm = (MyForm) form;
        myDS.unsubscribe(myForm.getEntityId());
    }
}

application-context-spring.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <!-- Enterprise layer's dependencies -->

    <bean id="springLocator"
        class="com.jraf.bootstrap.locator.SpringLocator">
    </bean>

    <!-- To precise the persistence configuration name file -->
    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>/META-INF/persistence-web.xml</value>
            </list>
        </property>
    </bean>

    <!-- EntityManagerFactory definition : JPA one -->
    <bean id="myEmf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager"
            ref="persistenceUnitManager" />
        <property name="persistenceUnitName" value="myPersistenceUnit" />
    </bean>

    <!-- TransactionManager JPA one -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>

    <!-- EntityManagerFactory definition : JPA one -->
    <bean id="myEmf2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
        <property name="persistenceUnitName" value="myPersistenceUnit2" />
    </bean> 

    <!-- Transaction Manager definition : JPA one-->
    <bean id="myTxManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf2" />
    </bean> 

    <!-- Enable annotation usage for transaction -->
    <tx:annotation-driven transaction-manager="transactionManager" />   

    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">      
    <property name="corePoolSize" value="1" />      
    <property name="maxPoolSize" value="1" />      
    <property name="queueCapacity" value="1" /> 
    </bean> 

    <bean id="myDS" class="com.my.internal.MyDS">
        <property name="myDao" ref="MyDAO"/>
    </bean>

    <!-- Enable the annotation usage (bean injection for instance) -->
    <context:annotation-config />
</beans>

我正在开发一个现有项目,这就是为什么取消订阅功能已经存在并使用本机查询而不是HQL查询的原因。但也许我应该用它来代替......?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我也面临类似的问题。 添加

tx:annotation-driven transaction-manager="transactionManager"

到你的spring上下文xml将解决问题。