小编Zai*_*han的帖子

当jackson序列化json对象时,如何防止hibernate5从延迟抓取?

我目前正在运行一个spring-boot应用程序,其中端点返回存储在数据库中的特定对象的Page.为了我们的目的,我们称该对象为"x".在"x"中,有一个设置为延迟取出的对象列表.

@Entity
@DynamicUpdate
class x {

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;

      @JsonIgnore
      @OneToMany(mappedBy = "x", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
      private List<y> lazilyFetchedList;

      @Override
      public Integer getId() {
           return id;
      }

      public void setId(Integer id) {
           this.id = id;
      }

      @JsonIgnore
      public List<y> getLazilyFetchedList() {
           return lazilyFetchedList;
      }

      public void setLazilyFetchedList(List<y> lazilyFetchedList) {
           this.lazilyFetchedList = lazilyFetchedList;
      }
}
Run Code Online (Sandbox Code Playgroud)

我在@JsonIgnore上面设置是因为我不希望lazilyFetchedList在GET呼叫时被发送到客户端.

我的问题是,尽管杰克逊作为查看JSON响应的客户端成功忽略了字段.但是,当序列化Java对象"x"时,hibernate仍然会获取额外的查询lazilyFetchedList(即使jackson没有使用结果). …

spring hibernate jackson spring-boot hibernate-5.x

3
推荐指数
1
解决办法
1629
查看次数

标签 统计

hibernate ×1

hibernate-5.x ×1

jackson ×1

spring ×1

spring-boot ×1