簡體   English   中英

如何轉換清單 <Object> JPA本機查詢訪問POJO

[英]How to convert List<Object> to POJO using JPA native Query

如何將對象列表轉換為josn(鍵,值)格式?

我有一堂名為ClientRT ,在那堂課上有四個字段,即

res_nclient_room_type_id, res_sclient_rt_desc,res_sclient_rt_name, res_sclient_rt_code

服務等級

public List<Object> callSP() throws IOException {   

        List<Object> crt=crtRepo.roomtype(60);          

        //ObjectMapper mapper = new ObjectMapper(); 

        //String rtobject= mapper.writeValueAsString(crt);

        return crt;
    }

知識庫

@Repository
public interface ClientRoomTypeRepository extends JpaRepository<ClientRoomType, Integer> {

    @Query(value = "select * from roomtype(:int_inst_id)", nativeQuery = true)
    List<Object> roomtype(@Param("int_inst_id")Integer int_inst_id);
}

對象清單

 [
      [
        1,
        "TEMPORARILY NOT ASSIGNED",
        "TEMPORARILY NOT ASSIGNED",
        "000"
      ],
      [
        2,
        "FACILITIES - AVAILABLE ROOM",
        "FACILITIES - AVAILABLE ROOM",
        "050"
      ],

如何轉換成這種格式

[
 {
    "res_nclient_room_type_id":1 , 
    "res_sclient_rt_desc": "TEMPORARILY NOT ASSIGNED", 
    "res_sclient_rt_name":"TEMPORARILY NOT ASSIGNED" , 
    "res_sclient_rt_code":"000" 

 },
 {
  "res_nclient_room_type_id":2 , 
  "res_sclient_rt_desc": "FACILITIES - AVAILABLE ROOM", 
  "res_sclient_rt_name":"FACILITIES - AVAILABLE ROOM" , 
  "res_sclient_rt_code":"050" 
 },
]

有人可以建議我該怎么做嗎?

我看到您的問題是JPA使用投影來做到這一點,使用基於接口的投影創建帶有字段的界面

interface ClientRT {

  Long getResNclientRoomTypeId();
  String getResSclientRtDesc();
  String getResSclientRtName();
  String getResSclientRtCode();
  }

詢問

 @Query(value = "select * from roomtype(:int_inst_id)", nativeQuery = true)
List<ClientRT> roomtype(@Param("int_inst_id")Integer int_inst_id);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM