SpringDataJpa寫原生sql遇到的問(wèn)題及解決
SpringDataJpa寫原生sql遇到的問(wèn)題
@Repository
public interface EduCourseDao extends JpaRepository<EduCourse,Long>, JpaSpecificationExecutor<EduCourse> {
//根據(jù)課程id查詢課程的確認(rèn)信息
@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);
}
因?yàn)樯婕暗狡渌?,所以返回類型使用List< Map<String,Object>> ,而不是用原先定義的vo類
每行最后的,與 " 之間要有一個(gè)空格,不然報(bào)錯(cuò)。
Spring data jpa 自定義SQL語(yǔ)句遇到錯(cuò)誤
Not supported for DML operations
今天在自定義一個(gè)Update語(yǔ)句時(shí)運(yùn)行遇到一個(gè)錯(cuò)誤,顯示Not supported for DML operations 也就是說(shuō)不支持DML操作。
我的UserRepository是繼承的PagingAndSortingRepository接口,在看了JPA的文檔之后,發(fā)現(xiàn)此接口不支持update事務(wù),所以需要在注解上添加@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);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java文件下載設(shè)置中文名稱的實(shí)例(response.addHeader)
下面小編就為大家分享一篇java文件下載設(shè)置中文名稱的實(shí)例(response.addHeader),具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
SpringBoot中使用Servlet的兩種方式小結(jié)
這篇文章主要介紹了SpringBoot中使用Servlet的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Java使用poi獲取不到docx表格中書(shū)簽的問(wèn)題及解決
這篇文章主要介紹了Java使用poi獲取不到docx表格中書(shū)簽的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
SpringBoot項(xiàng)目導(dǎo)入外部jar包的詳細(xì)指南
在開(kāi)發(fā)SpringBoot項(xiàng)目時(shí),我們經(jīng)常需要引入一些外部的jar包來(lái)增強(qiáng)項(xiàng)目的功能,這些jar包可能不是Maven中央倉(cāng)庫(kù)中的,或者我們想要使用特定版本的jar包,本文將詳細(xì)介紹如何在SpringBoot項(xiàng)目中導(dǎo)入外部jar包,需要的朋友可以參考下2024-10-10
JPA?查詢?cè)鶶QL轉(zhuǎn)換VO對(duì)象方式
這篇文章主要介紹了JPA?查詢?cè)鶶QL轉(zhuǎn)換VO對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot integration實(shí)現(xiàn)分布式鎖的示例詳解
常規(guī)項(xiàng)目都是采用Redission來(lái)實(shí)現(xiàn)分布式鎖,進(jìn)行分布式系統(tǒng)中資源競(jìng)爭(zhēng)加鎖操作,偶然發(fā)現(xiàn)SpringBoot中的integration也實(shí)現(xiàn)多種載體的分布式鎖控制,下面我們就來(lái)看看具體實(shí)現(xiàn)方法吧2023-12-12
Springboot全局異常捕獲及try catch區(qū)別解析
這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
MyBatisPlus批量添加的優(yōu)化與報(bào)錯(cuò)解決
MybatisPlus是一個(gè)高效的java持久層框架,它在Mybatis的基礎(chǔ)上增加了一些便捷的功能,提供了更加易用的API,可以大幅度提高開(kāi)發(fā)效率,這篇文章主要給大家介紹了關(guān)于MyBatisPlus批量添加的優(yōu)化與報(bào)錯(cuò)解決的相關(guān)資料,需要的朋友可以參考下2023-05-05
MyBatis中?@Mapper?和?@MapperScan?的區(qū)別與使用解析
本文介紹了SpringBoot中MyBatis的兩個(gè)常用注解:@Mapper和@MapperScan,@Mapper用于標(biāo)記單個(gè)Mapper接口,而@MapperScan用于批量掃描指定包下的所有Mapper接口,兩者都有各自適用的場(chǎng)景,選擇合適的注解可以提高開(kāi)發(fā)效率并使代碼更加簡(jiǎn)潔,感興趣的朋友一起看看吧2025-01-01

