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

2022-01-24 0 809
目录

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

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

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 SpringDataJpa写原生sql遇到的问题及解决 http://www.20zxx.cn/297456/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务