3

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 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Still it will fetch the parent entity records.
You need to do some configuration for that..!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.