Search code examples
javaspringprojection

Java Spring projection inside projection


Is it possible to use a projection and in some related object use it's own projection?

For example, a have Exam, that has List<Question>. I'd like to request a list of exams (which I have a @projection), but I'd like to define the attributes to be retrieved for each related Question


Solution

  • If I understand correctly you want to use Projection as children of Projection. If it is the case, yes, you can. You can create a QuestionProjection and use inside the ExamProjection.

    Example:

    @Projection(name = "questionProjection", types = { Question.class }) 
    public interface QuestionProjection {
        // Getters
    }
    
    @Projection(name = "examProjection", types = { Exam.class }) 
    public interface ExamProjection {
        List<QuestionProjection> getQuestionList();
    
        // Other Getters
    }