溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何解決SpringDataJpa寫原生sql遇到的問題

發(fā)布時(shí)間:2021-11-22 12:35:05 來源:億速云 閱讀:241 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下如何解決SpringDataJpa寫原生sql遇到的問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

SpringDataJpa寫原生sql遇到的問題

@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語句遇到錯(cuò)誤

Not supported for DML operations

今天在自定義一個(gè)Update語句時(shí)運(yùn)行遇到一個(gè)錯(cuò)誤,顯示Not supported for DML operations 也就是說不支持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);

以上是“如何解決SpringDataJpa寫原生sql遇到的問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI