简体   繁体   中英

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

How to convert List of object to josn (key,value) format?

I am having one class with name ClientRT and in that class four fields are there ie

res_nclient_room_type_id, res_sclient_rt_desc,res_sclient_rt_name, res_sclient_rt_code

Service class

public List<Object> callSP() throws IOException {   

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

        //ObjectMapper mapper = new ObjectMapper(); 

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

        return crt;
    }

Repository

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

List of Object

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

How to convert In this format

[
 {
    "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" 
 },
]

Can any one suggest me how I can do that?

I see your problem is with JPA use projection for doing that, create one interface with fields Interface-based Projections

interface ClientRT {

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

Query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM