S
S
sanek_gyy2018-10-24 19:27:35
Java
sanek_gyy, 2018-10-24 19:27:35

How does LAZY/EAGER loading work in practice?

I have the following code: two entities with a 1-to-many relationship ( library and book ),

@Entity
@Table(name = "book")
//@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  long id;

    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "library_id")
    @JsonBackReference
    private Library library;

// constructor, getter and setter

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Entity
//@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Library implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;

    @OneToMany(mappedBy = "library", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<Book> books;

// constructor, getter and setter

simple repository
and controller
@RestController
public class BookController {
    @Autowired
    BookRepository bookRepository;

    @GetMapping("/allbook")
    private Iterable<Book> getAllBook() {
        return bookRepository.findAll();
    }

    @GetMapping("/bookbyid")
    private Book getBookById() {
        Book book = bookRepository.findById((long) 1).get();
        return book; }

    @GetMapping("librarybybook")
    private Library getLibraryByBook() {
        Library library = bookRepository.findById((long) 1).get().getLibrary();
        return library;
    }
}

Question 1) Displaying all the books, I expected to see Id, name and library_id. But in the end I only get Id and name. This happens with any load of LAZY/EAGER. Why?
Question 2) When loading LAZY, trying to get the library to which book belongs, I get the error com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer...
and the screen displays the following
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Wed Oct 24 19:14:12 MSK 2018</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Type definition error: [simple type, class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.netcraker.entity.Library_$$_jvst22e_0[&quot;handler&quot;])</div></body></html>

But if you set (fetch = FetchType.EAGER), it outputs the library in place of the book associated with it.
I can't figure out why either.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question