sql - Spring JPA : No converter data from DB in to DTO
问题描述
I have a custom query in my Repository. And I created a DTO interface like this:
public interface ImagePathAndID {
String getImagePath();
Integer getIdProduct();
}
My query is :
@Query(value = "select image.image_path, product.product_id from image\r\n" +
" inner join product on product.product_id = image.product_id\r\n" +
" inner join category as c on product.category_id = c.category_id \r\n" +
" where c.category_id = :id ", nativeQuery = true)
public List<ImagePathAndID > selectAllImagePathForCategory(@Param("id") int id);
And when I return data I get null value for getImagePath
and getIdProduct
.
List<ImagePathAndID> imagePath = this.categoryRepository.selectAllImagePathForCategory(id);
for (ImagePathAndID image:imagePath ) {
System.out.println(image.getImagePath() + image.getIdProduct());
}
I got the 3 object for ImagePathAndID
but value for this object is null
.
Output is :
null,null
null,null
null,null
解决方案
这里的关键是定义的属性应该与接口方法命名约定完全匹配。参考文档
您应该修改您的查询和接口方法,如下所示。
询问
@Query(value ="select image.image_path as imagePath, product.product_id as productId from image\r\n" +
" inner join product on product.product_id = image.product_id\r\n" +
" inner join category as c on product.category_id = c.category_id \r\n" +
" where c.category_id = :id ", nativeQuery = true)
public List<ImagePathAndID > selectAllImagePathForCategory(@Param("id") int id);
界面
public interface ImagePathAndID {
String getImagePath();
Integer getProductId();
}
推荐阅读
- node.js - NojeJs Mongoose - 如果数据不存在,则将数组对象推入 mongo
- javascript - 如何在不使用 navigation.navigate() 的情况下在 React Native 中的屏幕之间导航?
- python - Python re 我想拆分所有标点符号,除了 - 符号
- php - Laravel 语言文件分离逻辑
- python - 将不同聚合函数应用于熊猫数据框的不同列的 Pythonic 方式?并有效地命名列?
- python - 以单列为中心,但具有对角线历史客户访问视图
- python - 如何合并多个 CSV 文件?
- firebase - Flutter异步firebase请求未等待
- docker - 如何自动清理 Amazon EKS 工作线程节点中的 Docker 资源?(模仿 docker 系统 prune -a)
- macos - 在 macOS 上的 SwiftUI 列表视图中选择和删除核心数据实体