Spring JPA:无转换器数据从DB输入到DTO

时间:2019-06-29 16:14:31

标签: sql postgresql spring-boot jpa

我的存储库中有一个自定义查询。我创建了这样的DTO界面:


sns.lineplot(data=file)
plt.show()

Dataframe.info() message
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 59 entries, 0 to 58
Data columns (total 5 columns):
Date                                    59 non-null object
Avila Adobe                             59 non-null int64
Firehouse Museum                        59 non-null int64
Chinese American Museum                 59 non-null int64
America Tropical Interpretive Center    59 non-null int64
dtypes: int64(4), object(1)
memory usage: 2.4+ KB

我的查询是:

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); getImagePath的空值。

getIdProduct

我得到了List<ImagePathAndID> imagePath = this.categoryRepository.selectAllImagePathForCategory(id); for (ImagePathAndID image:imagePath ) { System.out.println(image.getImagePath() + image.getIdProduct()); } 的3个对象,但该对象的值为ImagePathAndID。 输出为:

null

1 个答案:

答案 0 :(得分:2)

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

您应该像下面那样修改查询和接口方法。

查询

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

}