Answer the question
In order to leave comments, you need to log in
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
@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;
}
}
<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["handler"])</div></body></html>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question