【问题标题】:How to Enable/Disable Entity Relations in Runtime?如何在运行时启用/禁用实体关系?
【发布时间】:2021-06-29 02:21:14
【问题描述】:

我有一个具有基本 CRUD 服务的 Spring Boot 应用程序。对于阅读服务,我也想查看它们的关系。通过@ManyToOne、@OneToOne 等注解实现关系没有问题,如this 示例。

我的问题是我想根据列表服务中的参数启用这种关系,或者我也可以使用另一个端点。我怎样才能做到这一点?欢迎提出任何建议。

  • 参数版本可能像 -> /employe/list?includeRelations=true
  • 端点版本可能类似于 -> /employee/list/byRelations

我的实体是这样的;

    @Entity
    @Table(name = "employee")
    public class Employee{
        private long id;
        private String name;
        private Address address;
        
        // getter setters
    }
    
    @Entity
    @Table(name = "address")
    public class Address {
        private long id;
        private String name;
        private String postalCode;
    
        // getter setters
    }

编辑 例如

  • 如果没有 includeRelations=true '/employee/list' 服务应该返回这个;
{
    "id": 1,
    "name": "Jane"
}
  • 如果 includeRelations=true '/employee/list' 服务应该返回这个;
{
    "id": 1,
    "name": "Jane"
    "address": { 
                 "id":1,
                 "name": "HOME",
                 "postalCode": "11111"
                }
}

【问题讨论】:

  • 还是不清楚。启用这种关系是什么意思?给出情景。
  • @krishnkantjaiswal 我添加了一个示例。如果还不清楚,请告诉我。

标签: spring-boot spring-data-jpa spring-data spring-data-rest


【解决方案1】:

您可以使用 fetchType=Lazy 注释关系。然后调用 getter 手动加载所需的关系。 另一种选择是急切地加载所有关系,但使用 @JsonView 注释响应并排除您不需要的关系。

【讨论】:

    【解决方案2】:

    其中一种方法是返回不同的数据传输对象,具体取决于 REST 请求。

    假设您有以下类,除了实体。

    class EmployeeDto {
      private Long id;
      private String name;
    }
    
    class EmployeeAddressDto {
      private Long id;
      private String name;
      private AddressDto address;
    }
    
    class AddressDto {
      private Long id;
      private String name;
      private int postalCode;
    }
    

    然后在控制器中你会做这样的事情。

    @GetMapping("/employee/list")
    public ResponseEntity<?> getEmployees(@RequestParam int detailed) {
    
      if (detailed) {
        return employeeService.getDetailedEmployeeList();
      } else {
        return employeeService.getEmployeeList();
      }
    }
    

    服务接口看起来像这样。

    interface EmployeService() {
    
      List<EmployeeDto> getEmployeeList();
    
      List<EmployeeAddressDto> getDetailedEmployeeList();
    }
    

    您还需要处理实体以传输对象转换。

    【讨论】:

      【解决方案3】:

      它的一些sudo 代码供您理解。你可以使用Query Parameter 并且在条件下你可以调用你想要的repo: 对于我的场景,我想要不同的响应 shortmediumlong

      @RequestMapping(value = "/getContacts", 方法 = RequestMethod.POST, 产生 = MediaType.APPLICATION_JSON_VALUE, headers = "Accept=application/json") 公共字符串 getContact(@RequestBody ContactItemRequestInfo contactItemRequestInfo, @RequestParam(required = false) 字符串键, 字符串联系) {

          if(key.equals("medium"))
          {
               return Contact="{\"responseCode\":\"02\",\"responseDescription\":\"Success\",\"totalCount\":2,\"contacts\":[{\"id\":114,\"firstName\":\"ali\",\"lastName\":\"kamran\"},{\"id\":115,\"firstName\":\"usman\",\"lastName\":\"khan\",\"middleName\":\"saad\"}]}";
               
          }
          else if(key.equals("long"))
          {
              return Contact="{\"responseCode\":\"03\",\"responseDescription\":\"Success\",\"totalCount\":2,\"contacts\":[{\"id\":114,\"firstName\":\"ali\",\"lastName\":\"kamran\"},{\"id\":115,\"firstName\":\"usman\",\"lastName\":\"khan\",\"middleName\":\"saad\"}]}";
              
          }
          else
          {
              return Contact="{\"responseCode\":\"00\",\"responseDescription\":\"Success\",\"totalCount\":2,\"contacts\":[{\"id\":114,\"firstName\":\"ali\",\"lastName\":\"kamran\"},{\"id\":115,\"firstName\":\"usman\",\"lastName\":\"khan\",\"middleName\":\"saad\"}]}";
              
          }
      

      } 对你有帮助!!

      【讨论】:

        猜你喜欢
        • 2014-05-23
        • 1970-01-01
        • 2020-08-18
        • 1970-01-01
        • 2011-11-15
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多