春季预测选择集合

时间:2018-06-06 20:19:33

标签: spring-data spring-data-jpa spring-projections

我试图让电台投影包括相关徽标列表。以下是我的域名:

@Table(name = "Station")
public class Station implements Serializable {

@Id
@Column(name = "Id")
private int id;

@OneToMany(cascade = CascadeType.ALL,
        fetch = FetchType.LAZY,
        mappedBy = "station")
private Set<Logo> logos;
}

@OneToMany相关徽标:

@Table(name = "Logo")
public class Logo {

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

@Transient
private String fullUrl; // calculated

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "StationId", nullable = false)
private Station station;
}

我的存储库和查询如下:

@Query(value = "SELECT s.id AS Id, s.logos As Logos FROM Station s JOIN s.users su WHERE su.username = ?1")
Collection<StationListViewProjection> findStationsByUsername(String username);

我的电台投影需要Id和logoProjections列表

@Projection(name = "StationListViewProjection", types = Station.class)
public interface StationListViewProjection {
   int getId();
   Set<LogoProjection> getLogos();
}

logoProjection只需要网址

@Projection(name = "LogoProjection", types = Logo.class)
public interface LogoProjection {
   String getFullUrl();
}

当我执行查询时,我收到一个奇怪的回复:

MySQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在'as col_5_0_附近使用正确的语法。作为col_6_0_,stationlog3_.id为id1_20_0 _'

1 个答案:

答案 0 :(得分:0)

如果我明白这个

@Transient
private String fullUrl; // calculated

正确,您的fullUrl会在您的java代码中计算出来,更重要的是,它在数据库中没有匹配的列。您不能直接在投影中使用此类字段。您可以使用Open Projection并指定计算以使用fullUrl注释获取@Value