Spring:如何在投影中使用SpEL表达式

问题描述 投票:0回答:1

在投影ScheduledSessionWithDetail我想从其他链接表添加一个值为此我使用SpEL表达式,但它不工作的colorcode列,我想通过表达式不返回剩余的字段,谁能告诉我我错在哪里?

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();
}

S程度了session.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;
    }

水平

@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;
    }
java spring expression entity projection
1个回答
0
投票

您的ProgramSchedule实体似乎没有提及Level


此外,在使用投影时,您应该使用

#{target.programSchedule.level.colorCode}

而不是bean /参数名称。这在春季博客https://spring.io/blog/2014/05/21/what-s-new-in-spring-data-dijkstra中提到,并在https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.projections结尾处简要提及

© www.soinside.com 2019 - 2024. All rights reserved.