iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringDataJpa写原生sql遇到的问题及解决
  • 718
分享到

SpringDataJpa写原生sql遇到的问题及解决

2024-04-02 19:04:59 718人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录springDataJpa写原生sql遇到的问题Springdatajpa自定义SQL语句遇到错误NotsupportedforDMLoperationsSpringDataJp

SpringDataJpa写原生sql遇到的问题


@Repository
public interface EduCourseDao extends JpaRepository<EduCourse,Long>, JpaSpecificationExecutor<EduCourse> {
    //根据课程id查询课程的确认信息
    @Query(value = "SELECT ec.id,ec.title,ec.price,ec.lesson_num, " +
            "ecd.description, " +
            "es1.title AS oneSubject, " +
            "es2.title AS twoSubject " +
            "FROM edu_course ec LEFT JOIN edu_course_description ecd ON ec.id=ecd.id " +
            "LEFT JOIN edu_teacher et ON ec.teacher_id=et.id " +
            "LEFT JOIN edu_subject es1 ON ec.subject_parent_id=es1.id " +
            "LEFT JOIN edu_subject es2 ON ec.subject_id=es2.id " +
            "WHERE ec.id=?1 ",nativeQuery = true)
    List< Map<String,Object>> findPublishInfoById(String id);

}

因为涉及到其他表,所以返回类型使用List< Map<String,Object>> ,而不是用原先定义的vo类

每行最后的,与 " 之间要有一个空格,不然报错。

Spring data jpa 自定义SQL语句遇到错误

Not supported for DML operations

今天在自定义一个Update语句时运行遇到一个错误,显示Not supported for DML operations 也就是说不支持DML操作。

我的UserRepository是继承的PagingAndSortingRepository接口,在看了JPA的文档之后,发现此接口不支持update事务,所以需要在注解上添加@Modifying。

原文如下:

3.3.7. Modifying queries

All the sections above describe how to declare queries to access a given entity or collection of entities.Of course you can add custom modifying behaviour by using facilities described in Customimplementations for Spring Data repositories. As this approach is feasible for comprehensive customfunctionality, you can achieve the execution of modifying queries that actually only need parameterbinding by annotating the query method with @Modifying:

Example 45. Declaring manipulating queries

@Modifying
@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
This will trigger the query annotated to the method as updating query instead of a selecting one. As theEntityManager might contain outdated entities after the execution of the modifying query, we do notautomatically clear it (see JavaDoc of EntityManager.clear() for details) since this will effectively dropall non-flushed changes still pending in the EntityManager. If you wish the EntityManager to be clearedautomatically you can set @Modifying annotation's clearAutomatically attribute to true.

以下是我的源码:


@Modifying
@Query("update User u set u.u_name=?2,u.u_sex=?3,u.u_date = ?4,u.u_minzu = ?5,u.u_area=?6,u.u_country=?7 where u.u_id =?1")
public void updateUserById(String u_id,String u_name,String u_sex,
                               String u_date, String u_minzu,String u_area,
                               String u_country);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringDataJpa写原生sql遇到的问题及解决

本文链接: https://www.lsjlt.com/news/157968.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringDataJpa写原生sql遇到的问题及解决
    目录SpringDataJpa写原生sql遇到的问题Springdatajpa自定义SQL语句遇到错误NotsupportedforDMLoperationsSpringDataJp...
    99+
    2024-04-02
  • 如何解决SpringDataJpa写原生sql遇到的问题
    小编给大家分享一下如何解决SpringDataJpa写原生sql遇到的问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!SpringDataJpa写原生sql遇到...
    99+
    2023-06-25
  • 解决springdataJPA对原生sql支持的问题
    springdataJPA对原生sql支持问题 在项目中用到的是springdataJPA连接数据库进行操作,但是JPA中的hql语句不能够满足业务要求,因而需要用到原生sql 但是...
    99+
    2024-04-02
  • 怎么解决springdataJPA对原生sql支持的问题
    这篇文章主要为大家展示了“怎么解决springdataJPA对原生sql支持的问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么解决springdataJPA对原生sql支持的问题”这篇文章...
    99+
    2023-06-15
  • PyTorch 编写代码遇到的问题及解决方案
    PyTorch编写代码遇到的问题 错误提示:no module named xxx xxx为自定义文件夹的名字 因为搜索不到,所以将当前路径加入到包的搜索目录 解决方法: i...
    99+
    2024-04-02
  • 使用@ApiModel遇到的问题及解决
    目录@ApiModel遇到的问题1. 习惯2. 遇坑3. 排查4. 解决@ApiModel和@ApiModelProperty版本@ApiModel@ApiModelProperty...
    99+
    2024-04-02
  • 如何解决写gulp遇到的ES6问题
    这篇文章给大家分享的是有关如何解决写gulp遇到的ES6问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。前言Gulp.js 是一个自动化构建工具,开发者可以使用它在项目开发过程...
    99+
    2024-04-02
  • 【总结】sublime写PHP遇到的常见问题及解决方案
    Sublime Text是一款功能丰富的文本编辑器,它被广泛使用来编写各种编程语言的代码。然而,一些PHP开发者有时遇到了一些问题,他们发现Sublime Text可能对PHP不太管用。这令人感到困惑:为什么这个备受喜爱的编辑器在处理PHP...
    99+
    2023-05-14
    php sublime
  • docker-compose up -d遇到的问题及解决
    目录docker-compose up -d的问题docker-compose up -d <name>; No such service: <name>问题...
    99+
    2023-03-19
    docker-compose up -d docker-compose
  • sublime写PHP遇到的常见问题及解决方法是什么
    这篇文章主要讲解了“sublime写PHP遇到的常见问题及解决方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“sublime写PHP遇到的常见问题及解决方法是什么”吧!问题1:片段无...
    99+
    2023-07-05
  • SpringBoot @PathVariable使用时遇到的问题及解决
    目录@PathVariable使用时遇到的问题第一个问题解决办法第二个问题解决办法@PathVariable 404问题@PathVariable使用时遇到的问题 第一个问题 接口:...
    99+
    2024-04-02
  • Oracle更换为MySQL遇到的问题及解决
    目录迁移工具应用改造添加mysql8.0驱动包修改数据源配置对象改造问题汇总问题:本地远程连接mysql数据库,报10060登录异常问题:Navicat 连接MySQL8出现2059...
    99+
    2024-04-02
  • 基于restTemplate遇到的编码问题及解决
    目录背景问题一:中文乱码描述分析结论方案总结问题二:特殊字符串丢失描述分析结论方案背景 之前用restTemplate做网络间的请求,没遇到过问题。今天先是出现了中文乱码的问题,而后...
    99+
    2024-04-02
  • 使用@NonNull注解遇到的小问题及解决
    目录使用@NonNull注解遇到问题先简单介绍一下NotNull和NonNull的差异下面进入主题Lombok @NonNull注解总结使用@NonNull注解遇到问题 先简单介绍一...
    99+
    2023-01-06
    使用@NonNull注解 @NonNull注解 @NonNull注解问题
  • 解决springsecurity中遇到的问题
    目录spring security中遇到的问题1.An Authentication object was not found in the Security Context2.拦截...
    99+
    2023-01-28
    spring security spring security问题
  • C++std::initializer_list实现原理解析及遇到问题
     一般而言,对变量或对象使用括号初始化的方式被称为直接初始化,其本质是调用了相应的构造函数;而使用等号初始化的方式则被称为拷贝初始化,说到拷贝大家可能马上就会想到拷贝构造函...
    99+
    2024-04-02
  • Java 画时钟遇到的问题及解决方法
    这期内容当中小编将会给大家带来有关Java 画时钟遇到的问题及解决方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1.不能一直得到当前的时间,导致刷新时间不变。刚开始我自以为把int s = calen...
    99+
    2023-06-25
  • 安装vCenter6.0遇到的问题以及解决方法
    实验环境: 在Window Server 2008 R2上安装vCenter6.0,数据库采用的SQL Server2012   问题一: 安装到50%的时候提示:无法启动invsvc服务以及无法运行vdcpromo等各种问题,点...
    99+
    2023-06-04
  • Java 画时钟遇到的问题及解决方案
    1.不能一直得到当前的时间,导致刷新时间不变。 刚开始我自以为把int s = calendar.get(Calendar.SECOND)放到一个线程线程里再刷新就可以看到秒的变化了...
    99+
    2024-04-02
  • log4j升级log4j2遇到的问题及解决方式
    目录log4j升级log4j2的问题一、导入包二、在src/main/resources下新建一个log4j2.xml文件升级log4j2遇到的那些坑log4j升级log4j2的问题...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作