Get the App
SLTechnology News&Howtos  ›  Development  › 

The solution to the error report of @ Query annotations in SpringDataJpa

Shulou Source: shulou.com Published: 2022-06-02 07:27:27 09月30日 Update

This article will explain in detail the solution to the error reporting of @ Query comments on SpringDataJpa. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

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

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

HQL is written in the comment, so the object is queried, not the table name

Change to

Public interface TimeContentRepository extends JpaRepository {@ Query (value = "select id,user_id as userId,create_time as createTime" + "from TimeContent where create_time =? 1 and user_id =? 2") List 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}

For the same problem, the query field is also a member of the object, not a table field.

The SpringDataJpa @ query annotation uses native code to report an error

The query method of @ query native code has been used before, which can be used with normal annotations, probably in the form of:

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

As in the above form, the local query problem is solved perfectly.

However, this is queried using @ query native code as usual, and the program reports the following error:

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

The problem is found through problem analysis and online search:

@ query native queries cannot be used with pageable for paging queries. To solve this problem, you also want to use the pageable paging feature. The problem can be solved by modifying the native code in the following form:

@ Query (value= "from S_TC70 aac001=?1", countQuery= "select count (1) from S_TC70 aac001=?1") Page getUseOriginS_TC70 (String aac001,Pageable pageable); this is the solution to the @ Query comment on SpringDataJpa. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Tags: Query comment question code form method content field object article more knowledge article good function reason such as above member method program Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Microsoft NVidia Xiaomi