1、jpa自带的delete()方法可以成功删除对象

delete(id),或者delete(entity)

2、自定义删除方法注意点

参考:https://www.jianshu.com/p/9d5bf0e4943f

 @RequestMapping(value = "/delById")
public void delById(@RequestParam("id") int userId){
accountDao.delAccount(userId);
}

  注意,采用可以看到我们的@Query注解好像只是用来查询的,但是如果配合@Modifying注解一共使用,则可以完成数据的删除、添加、更新操作

public interface AccountDao  extends JpaRepository<Account,Integer> {

   // @Transactional
@Modifying
@Query(value = "delete from Account where id =?1",nativeQuery = true)
void delAccount(int id);
}

  可以看到报TransactionRequiredException

javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:54) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:238) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483) ~[spring-data-commons-1.13.1.RELEASE.jar:na]

在对应的方法上添加@Transactional 删除成功

public interface AccountDao  extends JpaRepository<Account,Integer> {

    @Transactional
@Modifying
@Query(value = "delete from Account where id =?1",nativeQuery = true)
void delAccount(int id);
}

  

jpa 自定义sql 删除方法注意点的更多相关文章

  1. spring boot系列(四)spring boot 配置spring data jpa (保存修改删除方法)

    spring boot 使用jpa在pom.xml在上文中已经介绍过.在这里直接介绍各个类文件如何编写: 代码结构: domain(存放实体类文件): repository(存放数据库操作文件,相当于 ...

  2. dede系统自定义变量删除方法

    之前添加了个联系电话的系统变量,忘记写描述,结果就显示个冒号,很难看.这样的就要删除了重来,那么织梦怎么删除添加的变量呢?其实很简单.两种方法: 第一种:执行SQL语句.在织梦后台执行-系统-SQL命 ...

  3. jpa自定义sql语句

    /** * 查询还没生成索引的帖子 * @return */ @Query(value = "SELECT * FROM t_article WHERE index_state=0" ...

  4. JPA中自定义的插入、更新、删除方法为什么要添加@Modifying注解和@Transactional注解?

    前几天,有个同事在使用JPA的自定义SQL方法时,程序一直报异常,捣鼓了半天也没能解决,咨询我的时候,我看了一眼他的程序,差不多是这个样子的: @Repository public interface ...

  5. Spring Boot 入门系列(二十七)使用Spring Data JPA 自定义查询如此简单,完全不需要写SQL!

    前面讲了Spring Boot 整合Spring Boot JPA,实现JPA 的增.删.改.查的功能.JPA使用非常简单,只需继承JpaRepository ,无需任何数据访问层和sql语句即可实现 ...

  6. SQL Server存储过程的删除方法

    SQL Server存储过程的删除方法使我们经常会用到的,下面就为您介绍扩展存储过程的删除方法,如果您对SQL Server存储过程方面感兴趣的话,不妨一看. --清除SQL Server所有的危险扩 ...

  7. 用sql删除数据库重复的数据的方法

      /***********************************************两个意义上的重复记录:1.是完全重复的记录,也即所有字段均重复的记录,2.是部分关键字段重复的记录, ...

  8. 用Springboot+Jpa实现学生CRUD操作(含前端页面,含分页,自定义SQL)

    前期准备 使用idea新建个SpringBoot项目 参考博客:https://blog.csdn.net/Mr_Jixian/article/details/89742366?tdsourcetag ...

  9. spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingExcept ...

随机推荐

  1. nodejs之express静态路由、ejs

    1.静态路由与ejs使用 /** *1.安装ejs npm install ejs --save-dev * *2.express 里面使用ejs ,安装以后就可以用,不需要引入 * *3.配置exp ...

  2. Windows监控——性能指标详解(转)

    http://blog.csdn.net/yiqin3399/article/details/51730106

  3. mxml 嵌入as代码出错,缺少 CDATA

    如果<mx:Script> 中有大于小于符号,代码必须包含在<![CDATA[ 之中,否则会报错

  4. Scratch少儿编程系列:(十一)Scratch编程之简单见解

    一.Scratch官网的说明     With Scratch, you can program your own interactive stories, games, and animations ...

  5. python基础--导入模块

    一,import的使用1, 模块就是一组功能的集合体,我们的程序可以导入模块来复用模块中的功能一个模块就是包含了一组功能的python文件,例如demo.py 可以通过import来使用这个文件定义d ...

  6. 配置tomcat-users.xml文件

    今天在学习登录日志保存时出现一系列错误,想查看浏览器后台的session,结果忘记怎么看用户名和密码了,下面是转载自民工也Coding的一篇文章, 文章链接为:http://www.cnblogs.c ...

  7. SQL修改数据表字段长度

    alter table m_Assysn_t nocheck CONSTRAINT allAlter Table m_Assysn_t ALTER column ppid VARCHAR(150)al ...

  8. Linux系统中使用Nignx配置反向代理负载均衡

    目录 使用nginx实现动静分离的负载均衡集群 使用nginx实现负载均衡和动静分离 使用nginx实现动静分离的负载均衡集群 Nginx官网源码包下载链接:http://nginx.org/en/d ...

  9. FESCAR

    FESCAR:阿里重磅开源分布式事务解决方案 FESCAR名字的由来:Fast & EaSy Commit And Rollback FESCAR是啥? 被用在微服务架构中的高性能分布式事务解 ...

  10. MySQL数据库创建时间和更新时间错乱问题

    在数据中勾选create_time和update_time不为空可以解决更新记录时,create_time也被更新的毛病