Spring 投影 select 集合

Spring projections select collection

我正在尝试让电台投影包含相关徽标列表。以下是我的域名:

@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只需要url

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

当我执行查询时,我得到了一个奇怪的响应:

MySQLSyntaxErrorException:您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在 'as col_5_0_, . as col_6_0_, stationlog3_.id as id1_20_0_'

附近使用的正确语法

如果我理解这个

@Transient
private String fullUrl; // calculated

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