在我的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>
....
我想在其中一个存储库中使用自定义本机查询
@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
答案 0 :(得分:5)
您应该像这样使用@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();
}
答案 1 :(得分:0)
你可以试试这个。根据您的业务需求更改查询和方法。希望它有效。
public interface UserRepository extends JpaRepository<User, Long> {
@Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?0", nativeQuery = true)
User findByEmailAddress(String emailAddress);
}