首页 > 解决方案 > Spring NamedNativeQuery 错误:“没有为该名称定义查询”

问题描述

我正在尝试使用 Spring 数据中的 NamedNativeQuery 并收到错误消息:

java.lang.IllegalArgumentException:没有为该名称定义查询 [测试]

我的查询:

package com.test.domain;

import javax.persistence.NamedNativeQuery;

@NamedNativeQuery(
        name = TestNativeNamedQuery.FIND_QUERY_NAME,
        query= "SELECT id, name FROM table_test",
        resultClass = TestNativeNamedQuery.class )
public class TestNativeNamedQuery {
    public static final String FIND_QUERY_NAME = "test";
    Integer id;
    String name;
    // getters and setters
}

查询运行器:

package com.test.service;

@Service
public class TestService {

    @PersistenceContext
    EntityManager em;
    
    public ResponseEntity run() {       
            Query query = em.createNamedQuery(
                TestNativeNamedQuery.FIND_QUERY_NAME, TestNativeNamedQuery.class);
                
            List result = query.getResultList();        
    }       
}

春季应用程序配置:

package com.test;

@SpringBootApplication(scanBasePackages = {"com.test"})
@EnableConfigurationProperties
@ConfigurationPropertiesScan(basePackages = {"com.test"})
@EnableJpaRepositories(basePackages = {"com.test"})
@EntityScan(basePackages = {"com.test.domain"})
@ComponentScan(basePackages = {"com.test"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

标签: javaspring-datanamed-query

解决方案


推荐阅读