【问题标题】:Spring:how to use SpEL expression in projectionSpring:如何在投影中使用SpEL表达式
【发布时间】:2018-06-06 04:50:19
【问题描述】:

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

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
}

节目安排

@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】:

    您的ProgramSchedule 实体似乎没有引用Level


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

    #{target.programSchedule.level.colorCode}

    而不是 bean/参数名称。这个在Spring博客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末尾也有简单的提到

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2011-09-29
      • 2017-07-20
      相关资源
      最近更新 更多