I'm using spring boot
and jpa
, I'm trying to get data from parent entity and child entity using jparepository
.
Parent Entity:
@Entity
@Table(name = "parent")
public class Parent {
@Id
private int id;
private String name;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private Set<Child> children;
}
Child Entity:
@Entity
@Table(name = "child")
public class Child {
@Id
private int id;
private String name;
private int parent_id;
@ManyToOne
@JoinColumn(name = "parent_id", referencedColumnName = "id")
private Parent parent;
jpaRepository:
public interface ParentRepository extends JpaRepository<Parent, Integer>, JpaSpecificationExecutor<Parent> {
}
the reason I set the fecth to FetchType.LAZY
is that sometimes I just want to get parent without child.
So, here is my question: when I use
parentRepository.findAll(pagable);
the result only contains parents, not child, but I want the result to contain children, and in some situation I don't want it. how to write it ?