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();        
    }       
}

Spring 应用配置:

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);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)