有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring boot在json中总是将延迟加载的字段显示为null

我正在开发一个Spring Boot应用程序,在这个应用程序中,我在类中懒洋洋地加载了一堆字段。然而,当在请求中返回对象时,这些字段是空的,即使它们应该有一个值

我在应用程序中添加了支持hibernate的对象映射器

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
  return new Jackson2ObjectMapperBuilder()
    .modulesToInstall(Hibernate4Module.class);
}

在代码中访问惰性加载的字段时,我可以很好地访问它们,但简单地将对象转换为json是行不通的

媒体内容。java

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class MediaContent extends Content {

  @OneToOne(fetch = FetchType.LAZY)
  private StoredFile previewFile;

  @OneToOne(fetch = FetchType.LAZY)
  private StoredFile imageFile;

  @OneToMany(fetch = FetchType.LAZY, mappedBy = "downloadFor")
  private List<StoredFile> downloadFiles;

  @ManyToOne(fetch = FetchType.LAZY)
  private Genre genre;

  ...
}

ContentController。java
这将返回包含所有延迟加载字段null的对象,即使这些字段在数据库中有值

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@Transactional
public @ResponseBody
Content find(@PathVariable Long id) {
  return em.find(Content.class, id);
}

基本上,我希望延迟加载的字段在将对象返回到请求时接收它们的值


共 (0) 个答案