FetchType.LAZY在休眠状态下不适用于@ManyToOne映射 [英] FetchType.LAZY not working for @ManyToOne mapping in hibernate

查看:204
本文介绍了FetchType.LAZY在休眠状态下不适用于@ManyToOne映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说,我在Child类和Parent类之间存在多对一的关系.我想加载所有子级,而不必加载其父级详细信息. 我的孩子班是

Simply put I have a many to one relation on the Child class with the Parent class. I want to load all the children without having to load their parent details. My child class is

@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() {
}
}

我有一个休息控制器,我想和他一起取所有没有父母的孩子

I have a rest controller with which I want to fetch all the children without their parents

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

当我运行它时,它会抛出

When i run this it throws

 {
  "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\"])\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)\n\tat org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)

我应该进行哪些更改,以便可以延迟加载父类?

What do I change so that I can lazy load the parent class?

提前谢谢.

推荐答案

您已经延迟加载了父类.发生异常是因为在加载父对象之前序列化了Child对象.要禁用它,您可以使用以下注释您的Child类: @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

You already lazy load the parent class. Exception happens because you serialize Child objects before parents are loaded. To disable it you can annotate your Child class with that: @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

在那之后,Parent在序列化中将被忽略.

After that Parent will be ignored in serialization.

这篇关于FetchType.LAZY在休眠状态下不适用于@ManyToOne映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆