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. 直连网(directly-connected networks)个数的计算

    直连网分为两种,point-to-point link和multiple access link, 如图: 对一个网络数直连网个数时,以上两种link都要计算.例子如下: 1. How many di ...

  2. zabbix服务端安装

    1.安装zabbix服务(1)先rpm安装lamp环境 yum install -y httpd mysql mysql-libs php php-mysql mysql-server php-bcm ...

  3. Windows 10 无法访问共享的解决办法大全

    本文前面介绍 Windows 10 操作系统无法访问其他电脑的共享文件夹,而其他电脑访问该共享可以访问的解决办法. 简单点说就是,你的操作系统是 Win10 ,你访问不了某台电脑的共享,但是别人可以. ...

  4. delphi中 formclose的事件 action:=cafree form:=nil分别是什么意思?

    MDI子窗体关闭时用到的(以下摘自Delphi的帮助)caNone  The form is not allowed to close, so nothing happens.caHide  The ...

  5. Windows服务器连接

    公司部分服务器是使用的Windows系统,需要远程连接 首先,我们要做的就是打开一个运行界面.按住win+r. 然后,我们在打开一行里输入mstsc. 这个时候我们点击确定,就会弹出一个远程桌面连接的 ...

  6. beego项目部署到nginx(含http转https)

    beego项目部署到nginx(含http转https)    之前的程序部署到服务器采用的直接部署,比较方便,现在把它部署到nginx,以便后续的反向代理和负载均衡,同时,因为要接入微信小程序,所以 ...

  7. ElasticSearch的中文分词器ik

    一.前言   为什么要在elasticsearch中要使用ik这样的中文分词呢,那是因为es提供的分词是英文分词,对于中文的分词就做的非常不好了,因此我们需要一个中文分词器来用于搜索和使用. 二.IK ...

  8. 跨域常见解决方案jsonp,cors示例

    ​​ 方案   JSONP jsonp需要后端进行配置,并且前端需要动态生成script标签通过callback调用函数进行操作的跨域解决方案 不建议使用该方案: 前后端均需进行配置处理,增加了工作量 ...

  9. P2802 【回家】

    (づ ̄3 ̄)づ╭❤-(题面哦~~) 当初做的时候也借鉴了一些题解,发现确实有很多人都是在n和m上分不清.. 好吧,我也没分清.. 然后就一直不停错,还找不出来原因.. 最后狠心把所有判断dfs停止的条 ...

  10. tensorflow 2.0 技巧 | 自定义tf.keras.Model的坑

    自定义tf.keras.Model需要注意的点 model.save() subclass Model 是不能直接save的,save成.h5,但是能够save_weights,或者save_form ...