Unexpected JSON Serialization implementing Hibernate 5 and Jackson on Lazy Loaded Entities
I'm migrating some code and I'm having trouble with I've been struggling with this for a few days now and could really use some help. I'm working with a frustrating scenario when trying to serialize a Hibernate entity using Jackson. I'm using Hibernate 5 and Jackson 2.12, and it seems that when I try to convert an entity with a lazy-loaded collection to JSON, I get an behavior stating `org.hibernate.LazyInitializationException: failed to lazily initialize a collection`. I've tried annotating the collection with `@JsonIgnore` to prevent serialization, but that leads to incomplete data being sent in the response. Hereβs a simplified version of my entity: ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) @JsonIgnore private List<Order> orders; // getters and setters } ``` When I make a GET request to fetch the user, I use the following controller method: ```java @GetMapping("/users/{id}") public ResponseEntity<User> getUser(@PathVariable Long id) { User user = userService.findById(id); return ResponseEntity.ok(user); } ``` The question occurs when I try to access the orders from the user entity, which should not happen since they are lazy-loaded. I want to include some order details in my JSON response without triggering the `LazyInitializationException`. Iβve looked into using DTOs, but that feels like an overhead for this use case. Is there a better way to handle this serialization scenario while still using lazy loading? Any suggestions would be greatly appreciated! For context: I'm using Java on Linux. What's the best practice here? I'm working on a application that needs to handle this. What's the best practice here? This is happening in both development and production on macOS. Thanks in advance! I've been using Java for about a year now. What are your experiences with this?