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

目录
  • SpringDataJpa写原生sql遇到的问题
  • Spring data jpa 自定义SQL语句遇到错误
    • Not supported for DML operations

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);

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

(0)

相关推荐

  • spring boot jpa写原生sql报Cannot resolve table错误解决方法

    错误如图 打开View→Tool Windows→Persistence选项 在弹出的Persistence窗口的项目上右键,选择Generate Persistence Mapping→By Database Schema 在此处进行数据库相关配置,配置成功后即可在下方看到数据库中的表 选择下载driver files,或者使用自己本地的connector 自定义连接 填写mysql相关的配置信息 选择data source,就可以看到数据库相关的配置了. 在弹出的Persistence窗口的

  • 聊聊Spring data jpa @query使用原生SQl,需要注意的坑

    目录 Spring data jpa @Query 使用原生Sql的坑 根据代码来解说: 需要注意的方法有以下几点 SpringData JPA @Query动态SQL语句 思路 实现 Spring data jpa @Query 使用原生Sql的坑 根据代码来解说: @Query(value = "select bill.id_ as id, bill.created_date as date, bill.no, lawyer_case .case_no as caseNo, " +

  • Spring Data JPA使用JPQL与原生SQL进行查询的操作

    1.使用JPQL语句进行查询 JPQL语言(Java Persistence Query Language)是一种和SQL非常类似的中间性和对象化查询语言,它最终会被编译成针对不同底层数据库的SQL语言,从而屏蔽不同数据库的差异. JPQL语言通过Query接口封装执行,Query 接口封装了执行数据库查询的相关方法.调用 EntityManager 的 Query.NamedQuery 及 NativeQuery 方法可以获得查询对象,进而可调用Query接口的相关方法来执行查询操作. JPQ

  • 解决springdataJPA对原生sql支持的问题

    springdataJPA对原生sql支持问题 在项目中用到的是springdataJPA连接数据库进行操作,但是JPA中的hql语句不能够满足业务要求,因而需要用到原生sql 但是有一个问题: @Query(value = "SELECT ppd.* FROM zt_productionplandetails AS ppd \n" + " \tLEFT JOIN zt_salesplan sp ON sp.id=ppd.salesPlan_id \n" + &qu

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

    目录 SpringDataJpa写原生sql遇到的问题 Spring data jpa 自定义SQL语句遇到错误 Not supported for DML operations SpringDataJpa写原生sql遇到的问题 @Repository public interface EduCourseDao extends JpaRepository<EduCourse,Long>, JpaSpecificationExecutor<EduCourse> { //根据课程id查

  • SpringDataJPA原生sql查询方式的封装操作

    工具类相关代码 使用到了apache的map2bean工具类 导入方法 <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> import org.apache.commons.beanutils.Bea

  • spring-data-jpa使用自定义repository来实现原生sql

    目录 使用自定义repository实现原生sql 自定义Repository接口 创建自定义RepositoryFactoryBean SpringDataJpa原生SQL查询 a.首先在StudentRepository里添加如下方法 b.在StudentController里面进行调用以上方法 使用自定义repository实现原生sql Spring Data JPA中的Repository是接口,是JPA根据方法名帮我们自动生成的.但很多时候,我们需要为Repository提供一些自定

  • thinkPHP框架中执行原生SQL语句的方法

    本文实例讲述了thinkPHP框架中执行原生SQL语句的方法.分享给大家供大家参考,具体如下: 怎样在thinkphp里面执行原生的sql语句? $Model = new Model();//或者 $Model = D(); 或者 $Model = M(); $sql = "select * from `order`"; $voList = $Model->query($sql); 只是需要new一个空的模型继承Model中的方法. 注意query是查功能,execute是增删改

  • 详解Java的Hibernate框架中的缓存与原生SQL语句的使用

    Hibernate缓存 缓存是所有关于应用程序的性能优化和它位于应用程序和数据库之间,以避免数据库访问多次,让性能关键型应用程序有更好的表现. 缓存对Hibernate很重要,它采用了多级缓存方案下文所述: 第一级缓存: 第一级缓存是Session的缓存,是一个强制性的缓存,通过它所有的请求都必须通过. Session对象不断自身的动力的对象,提交到数据库之前. 如果发出多个更新一个对象,Hibernate试图拖延尽可能长的时间做了更新,以减少发出的更新SQL语句的数量.如果您关闭会话,所有被缓

  • Django原生sql也能使用Paginator分页的示例代码

    django-pagination这是一个python包,来自github上的一个项目,很容易用. 不过这是一个懒人工具,好吧(工具理性).不过当一个页面有多处需要采用分页的话,就行不通了,要么修改django-pagination的源码,改变它的url指向,不过我没研究,当工程涉及到迁移时,要知道要安装各种东西本来就是个缺点,还要再修改源码,那就得不偿失.因而转战django自带的分页插件--Paginator. Paginator其实只需要实现两个方法`count`和`__getslice_

  • Laravel框架执行原生SQL语句及使用paginate分页的方法

    本文实例讲述了Laravel框架执行原生SQL语句及使用paginate分页的方法.分享给大家供大家参考,具体如下: 1.运行原生sql public function getList($data){ //获取前端传过来的参数 $user = $data['userId']; $office = $data['officeId']; $key = $data['oneKeySearch']; //进行模糊搜索和联合查询 $where = 'and 1=1 '; if($key!=null) {

  • 手把手教你用Django执行原生SQL的方法

    前言 Hey,各位小伙伴,这次怎么来玩一下,如何使用Django执行原生SQL. 我们都知道,Python在web界的扛把子--Django,可谓是集大成为统一,各种各样的插件.forms组件.model模型.Admin后台等等,后面我会专门出文章娓娓道来,反正就是一个字,NB. 本次就来学一下,如何在Django执行原生语句. 起因 在使用Django时,一般情况下,我们使用Django自带的model查询是没有问题的,基本能满足80%的问题 但是,但是,那20%就不要了吗???肯定不行哎,小

随机推荐