日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Spring Data Jpa 投影(Projection)的用法

發布時間:2023/12/15 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 Spring Data Jpa 投影(Projection)的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前關聯查詢的時候返回幾個字段的時候我用到的返回方式是創建一個VO利用構建方法進行賦值返回的

現在也可以使用投影的方式獲取

import org.springframework.beans.factory.annotation.Value;

public interface GenericProjection {

    @Value("#{target.identifier}")
    String getIdentifier();

    @Value("#{target.value}")
    String getValue();

    @Value("#{target.recordType}")
    String getRecordType();

    @Value("#{target.description}")
    String getDescription();
}

repository

 @Query("select d.identifier as identifier, d.value as value,d.recordType as recordType,h.description as description from GenericConfigDetails d ,GenericConfigHeaders h where d.recordType = h.recordType and d.recordType=?1 and d.active='Y' ")
    List<GenericProjection> findProjectionByRecordType(String recordType);

注意:記得要加as起別名,不然@Value 的target獲取不到值,不起的時候我試了一下報以下的錯

"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: EL1008E: Property or field 'value' cannot be found on object of type 'org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap' - maybe not public or not valid?; nested exception is com.fasterxml.jackson.databind.JsonMappingException: EL1008E: Property or field 'value' cannot be found on object of type 'org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap' - maybe not public or not valid? (through reference chain: com.znstms.core.response.JsonResponseExt["data"]->java.util.ArrayList[0]->com.sun.proxy.$Proxy186["value"])",
"path": "/test/test1"
}

總結

以上是生活随笔為你收集整理的Spring Data Jpa 投影(Projection)的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。