溫馨提示×

溫馨提示×

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

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

SpringDataJpa的@Query注解報錯的解決方法

發(fā)布時間:2021-12-08 13:27:14 來源:億速云 閱讀:477 作者:柒染 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)SpringDataJpa的@Query注解報錯的解決方法,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

SpringDataJpa @Query注解報錯

public interface TimeContentRepository extends JpaRepository<TimeContent,String> {
    @Query(value = "select id,user_id as userId,create_time as createTime " +
            "from time_content where create_time = ?1 and user_id = ?2")
    List<TimeContent> findOnDay(String create_time,String userId);
}

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: time_content is not mapped

注解中寫的是HQL,所以查詢的是對象,而不是表名

改為

public interface TimeContentRepository extends JpaRepository<TimeContent,String> {
    @Query(value = "select id,user_id as userId,create_time as createTime " +
            "from TimeContent where create_time = ?1 and user_id = ?2")
    List<TimeContent> findOnDay(String create_time,String userId);
}

Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode: 'user_id' {originalText=user_id}

同樣的問題,查詢的字段也是對象的成員,不是表的字段

SpringDataJpa @query注解使用原生代碼報錯

之前用過@query 原生代碼的查詢方式,正常加注解就可以使用,大概形式為:

 @Query(value="select * from table",nativeQuery=true)
   K_KC54 getK_KC54UsingOriginSQL(String aac001);

如上形式,完美解決本地查詢問題。

但是,這是和往常一樣使用@query 原生代碼查詢,程序報如下錯誤:

org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries...

經(jīng)過問題分析與網(wǎng)上查找原因發(fā)現(xiàn)問題:

@query 原生查詢不能和分頁查詢的pageable一起使用。為解決這一問題,還想使用pageable分頁功能。修改原生代碼如下形式即可解決問題:

@Query(value="from S_TC70  aac001=?1 "
,countQuery="select count(1) from S_TC70  aac001=?1 ")
Page<S_TC70> getUseOriginS_TC70(String aac001,Pageable pageable);

關(guān)于SpringDataJpa的@Query注解報錯的解決方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

AI