这意味着我可以自定义结果对象,并将其映射到除实体类属性
之外的连接结果entityA { FIELDA fieldB }
entityB { FIELDA fieldC }
结果{ FIELDA fieldB fieldC }
@Query(从entityA a,entityB b中选择a。*,b.c,其中a.fieldA = b.fieldA) 结果customizeResult = entityARepository.nativeQuery()
如何获得包含3个字段的结果?
答案 0 :(得分:0)
如果我理解正确你想要返回自定义响应而不是你的实体对象本身。您可以为控制器使用ResponseBody批注,使用Jackson objectMapper来映射实体对象字段。假设你有MyEntity作为Entity对象,下面的代码将适合你。您还需要定义MyCustomResponse类并定义要在响应中显示的字段。
@RequestMapping(value = "/", method = RequestMethod.POST)
public @ResponseBody MyCustomResponse createEntity(@RequestBody String requestBody) {
ObjectMapper mapper = new ObjectMapper();
MyCustomResponse response = new MyCustomResponse()
MyEntity model = new MyEntity();
model = mapper.readValue(requestBody, MyEntity.class);
response.setSomething("");
response.setAnotherThing("");
response.setMyEntity(model);
return response;
}