解决 TransactionRequiredException:Executing an update/delete query

用spring data jpa写了个测试代码,

dao如下:

public interface UserDao extends CrudRepository<UserInfo, Long> { 

   @Query("from UserInfo where userid=?1")
public UserInfo getUserById(int userid);
   
   @Query("from UserInfo user where user.username= :username")
   public UserInfo getUesrByName(@Param("username") String username);
   
//    @Modifying
//    @Query("Delete from UserInfo  where userid= :userid")
//    public void delbyUserid(@Param("userid")long userid);




spring的配置文件如下;

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">


    <context:component-scan base-package="com.demo">
    <context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <tx:annotation-driven transaction-manager="transactionManager"/>


    <jpa:repositories base-package="com.demo.dao"  repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>


    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false"/>
                <property name="showSql" value="true"/>
            </bean>
            </property>
    </bean>


</beans>


使用了jpa进行事务管理,在dao中添加delbyuserid的方法后,运行的时候总是提示解决  TransactionRequiredException:Executing an update/delete query,因为CrudRepository就自带了几个增删改查的方法,所以我就是用了它自带的delete方法,就正常,不知道要在dao中添加自己的修改方法,要怎么做,有待去探索。

你可能感兴趣的:(spring,jpa,Data)