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 (将#修改为@)