微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring-data-jpa - 限制对象填充的深度

如何解决spring-data-jpa - 限制对象填充的深度

我有以下实体:

@Entity
@Table(name = "business",schema = "public")
public class Business {
  // some properties
}

@Entity
@Table(name = "appuser",schema = "public")
public class AppUser implements UserDetails {
  // some properties

  @JsonManagedReference
  @OnetoMany(mappedBy = "user",fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
  private List<UserBusinessRole> userBusinessRoles = new ArrayList<>();
}

@Entity
@Table(name = "appuser_business_role",schema = "public")
public class UserBusinessRole {
   // some properties

   @ManyToOne
   @JoinColumn(name = "business_id")
   private Business business;
}

这些在单独调用时没有问题,但是,我也有一个具有业务和应用程序用户的实体:

@Entity
@Table(name = "import_session",schema = "public")
public class ImportSession {
  // some properties

  @JsonIgnore
  @ManyToOne()
  @JoinColumn(
      name = "requester_user_id",referencedColumnName = "id"
  )
  private AppUser requester;

  @ManyToOne
  @JoinColumn(name = "business_id")
  private Business business;
}

但它为如下业务返回重复值(在角色和根对象下列出):

{
  "id": 14,...
  "requesterDto": {
    "id": 123,"emailAddress": "bar@bar.com","userBusinessRolesDto": [
      {
        "id": 6,"type": "ADMIN","businessDto": {
          "name": "Foo Inc"
          ...
        }
      }
    ]
  },"businessDto": {
    "name": "Foo Inc"
  }
}

有没有办法让它只返回某些字段,或控制它填充的“深度”,而无需大量手动修改/创建单独的 DTO?例如,它看起来像这样:

{
  "id": 14,"emailAddress": "bar@bar.com"
  },"businessDto": {
    "name": "Foo Inc"
    ...
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。