spring-boot - 在 Spring-Boot 中将 nativeQuery=true 与 @Query 一起使用不起作用

标签 spring-boot spring-data-jpa

在我的 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/

相关文章:

java - 如何修复 SpringApplication 上的 NoClassDefFoundError (Pivotal)

java - 如何设置 Spring/HIbernate JPA @Entiry 创建自动递增列(无 id)

spring - 使用 Spring Cloud FeignClients 时出现 404

java - Spring Boot 总是显示 @Value 注解的值为 null

java - Spring CrudRepository 按结果返回分组作为 Map

java - org.hibernate.hql.internal.ast.ErrorCounter - 第 1 行 :199: unexpected token: JOIN

java - Spring Data REST Controller 不接受 JSON 参数

java - JPA:使用虚拟 ID 将 View 映射为实体

jax-rs - 基于 JaxRs 的 Swagger 的 spring boot 应用程序配置

java - 处理 Kafka Broker 宕机时的故障