java - 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);
}
}
解决方案
推荐阅读
- c# - Google Firestore 获取数据库实例时出错
- google-chrome - Chrome 调试器模拟内存限制
- r - 修复 R 中邮政编码到县转换的数据集
- excel - 根据另一个日期单元格删除单元格
- datagrid - 复杂对象的 Blazorise Datagrid 内联编辑
- javascript - 如何在laravel循环中添加javascript
- ios - SwiftUI 错误:无法转换“UIImage”类型的值?关闭结果类型“无效”
- visual-studio-code - 我可以在 VS Code difftool 模式下突出显示字符吗?
- go - Golang:如何恢复字符串(int64)?
- java - 为什么这个getter函数只返回初始值