'Lazy loading is not working in OnetoMany Jpa relations
Below are my entity classes Instructor and Courses having OnetoMany Relations, where all CRUD Operation is working but while fetching Instructor I don't want courses to be fetched. But when I execute GET instructor By Id, It also fetches courses in Postman. This should be avoided.
public class Instructor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private String firstName;
private String lastName;
private String email;
@OneToMany(mappedBy = "instructor", cascade = {CascadeType.ALL})
@JsonManagedReference
private List < Course > courses;
}
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToOne(fetch =FetchType.LAZY)
@JoinColumn(name = "instructor_id")
@JsonBackReference
private Instructor instructor;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
