Fix lazy loading exception in REST endpoint

Problem Context

Users reported a LazyInitializationException when accessing the /api/departments endpoint. A developer changed the fetch type to EAGER to fix it. The entity is used in multiple places throughout the application.

Advanced
200 points

File Changes (1)

src/main/java/com/example/entity/Department.java MODIFIED
@@ -1 +1 @@
1 1 @Entity
2 2 @Table(name = "departments")
3 3 public class Department {
4 4 @Id
5 5 @GeneratedValue(strategy = GenerationType.IDENTITY)
6 6 private Long id;
7 7
8 8 private String name;
9 9
10 - @OneToMany(mappedBy = "department", fetch = FetchType.LAZY)
10 + // Fixed LazyInitializationException by changing to EAGER
11 + @OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
11 12 private List<Employee> employees = new ArrayList<>();
12 13
13 14 // getters and setters
14 15 }
Login Required: You must be registered to submit reviews and receive AI feedback. Register or login to start reviewing!

Your Review

Tip: Be thorough! Consider security, performance, code quality, and best practices.
Review Tips
  • Look for security vulnerabilities (SQL injection, XSS, etc.)
  • Check for null pointer exceptions and error handling
  • Consider performance implications
  • Evaluate code maintainability and readability
  • Check for proper resource management
  • Look for logic errors or edge cases
Analyzing Your Review
Our AI is carefully evaluating your code review against best practices