java - Spring:如何在投影中使用SpEL表达式
问题描述
在投影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();
}
调度会话.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;
}
解决方案
您的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
推荐阅读
- amazon-web-services - 无法连接到 Heroku CLI
- swift - 如何旋转 SCNNode 跟随线
- python - 为什么 jupyter notebook 不接受我的 csv 文件链接?
- typescript - forEach 循环中设置的变量类型错误
- html - 如何在智能手机的小屏幕上垂直或横向制作响应式导航堆栈(使用 Bootstrap 4 alpha 2)?
- python - 如何使用 QtWidgets 将 PyQtGraph 添加到 QMainWindow?
- python - 现场未找到 XPATH
- kubernetes - 无法运行 Kops 模板
- android - Mapbox SDK for android 安装不正确
- sql - SQL DATETIMEOFFSET (Eventdatetime) - 从字符串转换日期和/或时间时转换失败