SFAH
SFAH

Reputation: 534

Spring:how to use SpEL expression in projection

In projection ScheduledSessionWithDetail I want to add a value from other linked table for this I am using SpEL expression but its not working colorcode column that I want to get through expression not returning with remaining fields, can anyone tell me where I am wrong ?

ScheduledSessionWithDetail

@Projection(name="ScheduledSessionWithDetail",types=ScheduleSession.class)
public interface ScheduledSessionWithDetail {

    Long getId();

    int getStartTime();

    int getEndTime();

    DayOfWeek getDay();

    User getCoach();

    @Value("#{scheduleSession.programSchedule.level.colorCode}")
    String colorCode();
}

SchduleSession.java

@Entity
public class ScheduleSession {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long id;

    private int startTime;

    private int endTime;

    private boolean enabled=true;

    @OneToOne
    private User coach;

    @ManyToOne
    private ProgramSchedule programSchedule;

    @Enumerated(EnumType.STRING)
    private DayOfWeek day;
//getter and setter
}

ProgramSchedule

@Entity
public class ProgramSchedule {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long id;

    private String name;

    @JoinColumn(name="venue_id")
    @ManyToOne
    private Venue venue;

    @JoinColumn(name="program_id")
    @ManyToOne
    private Program program;

    private boolean enabled=true;

    @OneToOne
    private Term term;
    }

Level

@Entity
public class Level{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String level;

    private int minimumAge=0;

    private int maximumAge=0;

    private int duration=0;

    private int capacity=0;

    private String colorCode;
    }

Upvotes: 3

Views: 2443

Answers (1)

Michal
Michal

Reputation: 2273

Your ProgramSchedule entity doesn't seem to have a reference to Level


Additionally, when working with projections you should use

#{target.programSchedule.level.colorCode}

instead of bean/parameter name. This is mentioned in Spring blog https://spring.io/blog/2014/05/21/what-s-new-in-spring-data-dijkstra and also briefly mentioned at the end of https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projections

Upvotes: 2

Related Questions