【问题标题】:Failed to lazily initialize a collection of role , Spring懒惰初始化角色集合失败,Spring
【发布时间】:2020-05-27 19:03:04
【问题描述】:

我正在尝试从 Spring 中的 API 获取信息,但它给了我一个错误,因为其中一个字段是 Set。如何获取包含所有信息的 json?如果我使用 JsonIgnore,它不会给我我需要的集合,对吧?

我的班级:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "saloon")
public class Saloon implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;


    @OneToMany(mappedBy = "saloon")
    private Set<Service> services;


...

【问题讨论】:

    标签: json spring hibernate lazy-evaluation


    【解决方案1】:

    默认情况下,集合值属性是延迟获取的。一种简单的解决方案是将其更改为

    @OneToMany(mappedBy = "saloon", fetch = FetchType.EAGER)
    

    但这可能会在各种情况下导致严重的性能问题。

    要获得最佳解决方案,您需要分析您的设计,即您如何在代码中使用此属性。

    你可以看看OpenEntityManagerInViewInterceptor

    【讨论】:

      猜你喜欢
      • 2011-10-23
      • 2011-07-21
      • 2017-03-11
      • 1970-01-01
      • 2019-01-23
      • 2018-05-31
      相关资源
      最近更新 更多