I'm currently working on my lab in university. My professor told me to replace long JpaRepository methods (like findXWhereAnd) with Criteria Api. I have multiple entities with different join conditions and fetch strategies, something like this:
@Entity
@Data
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Child> children;
}
@Entity
@Data
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;
.
}
Now, I need to create a Criteria API query that fetches the Parent entities with their children list, while applying conditions on both of them.
How can I create such a query and avoid N+1 problem?
I've tried variations of criteria methods, but failed to understand how to do this.