在我的 Spring Boot 项目中,我有这个 pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
....
我想在其中一个存储库中使用自定义 native 查询
@Query(nativeQuery=true, "select * from question q where q.id < 5")
public Collection<QuestionEntity> queryAnnotated();
但是当我想给参数 nativeQuery = true 时,我得到一个语法错误
Syntax error on token ""select * from question q where q.id < 5"", invalid MemberValuePair
最佳答案
你应该像这样使用你的@Query:
@Query(nativeQuery = true, value="select * from question q where q.id < 5")
public Collection<QuestionEntity> queryAnnotated();
value
您的示例中缺少标签。 question
表和列q.id
应该与您的数据库的表名和列名完全匹配。在我的测试应用程序中,它有效:
@Repository
public interface QuestionRepository extends JpaRepository<Question, long>{
@Query(nativeQuery=true, value="SELECT * FROM Question q where q.id < 5")
public Collection<Question> findQuestion();
}
关于spring-boot - 在 Spring-Boot 中将 nativeQuery=true 与 @Query 一起使用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34744033/