您好,登錄后才能下訂單哦!
這篇文章主要介紹了JPA @Query時(shí)無法使用limit函數(shù)如何解決的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇JPA @Query時(shí)無法使用limit函數(shù)如何解決文章都會有所收獲,下面我們一起來看看吧。
使用ssh時(shí),我加入了springdata-jpa去查詢sql。
在@query中使用limit函數(shù)時(shí),報(bào)錯,后來分析原因才知道,springdata-jpa的@query中寫的sql叫JPQL,jpql是不支持limit函數(shù)的。
而原生sql是支持limit函數(shù)的,那我們?nèi)绾卧趒uery里寫原生sql呢?
@Query(nativeQuery=true,value = "")
value里寫正常sql語句
如果返回的是數(shù)據(jù)庫對應(yīng)的實(shí)體對象,那么sql的返回結(jié)果集字段別名中應(yīng)該與該實(shí)體類中對應(yīng)的數(shù)據(jù)庫字段名一致,可以有多余字段,但不能少字段
public interface GaidDao extends LogicDeleteableRepository<Gaid, Long>, JpaSpecificationExecutor<Gaid> { //加入:nativeQuery注解時(shí),寫原生sql,支持limit函數(shù) //不加入:nativeQuery注解時(shí)是JPQL。JPQL不支持limit函數(shù) @Query(nativeQuery=true, value = "select gaid from sdp_gaid where geo = ?1 and deleted = 0 order by id desc limit 1000000") List<String> findGaidByGeo(String geo); }
所有要使用原生的sql語句
@Query(value = "select * from d_quarterly_report where report_type=?1 order by cycle_number desc limit 1" ,nativeQuery = true) QuarterlyReport getLastUploadByReportType(Boolean type);
而不是
@Query(value = "select * from d_quarterly_report where report_type=?1 order by cycle_number desc limit 0,1",nativeQuery = true,) QuarterlyReport getLastUploadByReportType(Boolean type);
jpa中不支持limit 0,1的寫法只能寫成limit 1;
關(guān)于“JPA @Query時(shí)無法使用limit函數(shù)如何解決”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“JPA @Query時(shí)無法使用limit函數(shù)如何解決”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。