gpt4 book ai didi

sql - Spring JPA : No converter data from DB in to DTO

转载 作者:行者123 更新时间:2023-11-29 14:26:57 26 4
gpt4 key购买 nike

我的存储库中有一个自定义查询。我创建了一个这样的 DTO 接口(interface):

public interface ImagePathAndID {
String getImagePath();
Integer getIdProduct();
}

我的查询是:

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

当我返回数据时,我得到了 getImagePathgetIdProduct 的空值。

List<ImagePathAndID> imagePath = this.categoryRepository.selectAllImagePathForCategory(id);


for (ImagePathAndID image:imagePath ) {
System.out.println(image.getImagePath() + image.getIdProduct());
}

我得到了 ImagePathAndID 的 3 对象,但此对象的值为 null。输出是:

null,null
null,null
null,null

最佳答案

这里的关键是定义的属性要与接口(interface)方法的命名约定完全匹配。引用documentation

您应该像下面这样修改您的查询和接口(interface)方法。

查询

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

关于sql - Spring JPA : No converter data from DB in to DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56819031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com