FetchType.LAZY not working for @ManyToOne mapping in hibernate

ⅰ亾dé卋堺 提交于 2020-01-25 09:27:06

问题


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
}

My parent class is

@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?

Thanks in advance.


回答1:


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"})

After that Parent will be ignored in serialization.



来源:https://stackoverflow.com/questions/54828498/fetchtype-lazy-not-working-for-manytoone-mapping-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!