【问题标题】:Does Spring Data JPA support join resultSpring Data JPA 是否支持连接结果
【发布时间】:2017-10-31 02:47:44
【问题描述】:

这意味着我可以自定义结果对象,并将其映射到实体类属性之外的连接结果

实体A{ 字段A 字段B }

实体B{ 字段A 场C }

结果{ 字段A 字段B 场C }

@Query(select a.*,b.c from entityA a,entityB b where a.fieldA = b.fieldA) 结果customizeResult = entityARepository.nativeQuery()

如何获得 3 个字段的结果?

【问题讨论】:

    标签: spring-data-jpa


    【解决方案1】:

    如果我理解正确,您希望返回自定义响应而不是您的实体对象本身。您可以为控制器使用 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;
    }
    

    【讨论】:

    猜你喜欢
    • 2013-09-17
    • 1970-01-01
    • 2019-03-13
    • 2016-03-25
    • 1970-01-01
    • 2012-04-07
    • 2018-06-20
    • 1970-01-01
    • 2022-09-25
    相关资源
    最近更新 更多