Spring数据Truncate TransactionRequiredException执行更新/删除查询

时间:2017-12-05 08:48:56

标签: oracle spring-data spring-data-jpa truncate jta

回购定义如下:

import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.thy.crewdb.jpa.Schedule;

public interface ScheduleRepo extends CrudRepository<Schedule, String> {

   @Query(value = "TRUNCATE TABLE SCHEDULE", nativeQuery = true)
   @Modifying
   @Transactional
   void truncate();

}

应用程序上下文定义如下:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="xJndi" />
    <property name="resourceRef" value="true" />
</bean>

<!-- ################################################################################################################ -->

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.x.jpa" />
    <property name="jtaDataSource" ref="dataSource" />
    <property name="validationMode" value="NONE" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="ORACLE" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>

            ....

            <prop key="hibernate.transaction.auto_close_session">true</prop>
            <prop key="hibernate.connection.release_mode">auto</prop>
            <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform
            </prop>

        </props>
    </property>
</bean>

<!-- ################################################################################################################ -->

<tx:jta-transaction-manager />


<jpa:repositories base-package="com.x.repo" />

当我尝试调用truncate方法时,它会给出“javax.persistence.TransactionRequiredException:执行更新/删除查询”。我也尝试使用spring transactional anotation,它不起作用。

1 个答案:

答案 0 :(得分:0)

似乎您应该使用

import org.springframework.transaction.annotation.Transactional;

代替

import javax.transaction.Transactional;