fetch subclasses and their associations with one query

demo for simplicity and to avoid being sued :GnuTrolling:

so the question is whether its possible to fetch all the associations to their respective classes when calling the find all method

without eager fetching and avoiding n+1 and without extra queries for getting the associations

public interface ShapeRepository extends JpaRepository<Shape, Long> {
    Page<Shape> findAll();
}

@DiscriminatorValue("SQUARE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Square extends Shape {
    @OneToMany(mappedBy = "square", fetch = FetchType.LAZY)
    private Set<Vertices>;
}


@DiscriminatorValue("TRIANGLE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Triangle extends Shape {
    @OneToMany(mappedBy = "triangle", fetch = FetchType.LAZY)
    private Set<Angles>;
}


@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public abstract class Shape {
    @Id
    private long id;

    private String name;
}


@DiscriminatorValue("RECTANGLE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Rectangle extends Shape {
    @OneToMany(mappedBy = "rectangle", fetch = FetchType.LAZY)
    private Set<Sides>;
}
Was this page helpful?