SpringDataJpa的@Query注解報錯的解決
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);
順利解決問題!以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis Mybatis-Plus傳入多個參數(shù)的處理方式
這篇文章主要介紹了Mybatis Mybatis-Plus傳入多個參數(shù)的處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot利用dynamic-datasource-spring-boot-starter解決多數(shù)據(jù)源問題
dynamic-datasource-spring-boot-starter 是一個用于在 Spring Boot 項目中實現(xiàn)動態(tài)數(shù)據(jù)源切換的工具,下面我們看看如何使用dynamic-datasource-spring-boot-starter解決多數(shù)據(jù)源問題吧2025-03-03
Spring Cloud Feign實現(xiàn)文件上傳下載的示例代碼
Feign框架對于文件上傳消息體格式并沒有做原生支持,需要集成模塊feign-form來實現(xiàn),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2022-02-02

