首页 > 解决方案 > 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

标签: sqlpostgresqlspring-bootjpa

解决方案


这里的关键是定义的属性应该与接口方法命名约定完全匹配。参考文档

您应该修改您的查询和接口方法,如下所示。

询问

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

}


推荐阅读