fetchtype.lazy不为@manytoone映射在Hibernate中工作



简单地说,我在子类中与父母有很多关系。我想加载所有孩子,而不必加载父母的详细信息。我的孩子课是

@Entity
public class Child implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentId", referencedColumnName = "id")
private Parent parent;
public Child() {
}
// Getters and setters here
}

我的父班是

@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Parent() {
}
}

我有一个休息控制器,我想在没有父母的情况下取得所有的孩子

@GetMapping
public List<Child> getChildren() {
    return childRepository.findAll();
}

当我运行它时,它会抛出

 {
  "timestamp": "2019-02-22T13:45:31.219+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.example.attendance.models.Child["parent"]->com.example.attendance.models.Parent$HibernateProxy$1QzJfFPz["hibernateLazyInitializer"])",
  "trace": "org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.example.attendance.models.Child["parent"]->com.example.attendance.models.Parent$HibernateProxy$1QzJfFPz["hibernateLazyInitializer"])ntat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)ntat org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)ntat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)ntat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)

我要更改什么,以便我可以懒惰加载父级?

预先感谢。

您已经懒惰加载父类。发生例外是因为您在父母加载之前将子对象序列化。为了禁用它,您可以用它注释您的孩子课程: @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

在序列化中将忽略该父的。

最新更新