【发布时间】:2019-04-08 08:40:38
【问题描述】:
在 Spring Boot 应用程序中,我有一个复合 id 定义如下的类:
@Embeddable
public class StatisticId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(length = 255, nullable = false)
private String shortName;
@Enumerated(EnumType.STRING)
@Column(length = 32, nullable = false)
private Month month;
@Column(nullable = false)
private int year;
// getters, setters, equals, hashCode, toString
}
(简化的)类定义是:
@Entity
public class Statistic implements Serializable {
private static final long serialVersionUID = 1L;
private BigDecimal sales;
@EmbeddedId
private StatisticId id;
// getters, setters, toString
}
我想对这个类做一个投影:
public interface StatisticProjection {
public String getShortName();
public Month getMonth();
public int getYear();
public BigDecimal getSales();
}
并在以下存储库中使用它:
public interface StatisticsRepository extends CrudRepository<Statistic, Long> {
@Query(value = "select short_name, month, year, sales from statistic where short_name in = ?1", nativeQuery = true)
Iterable<StatisticProjection> findByShortName(Collection<String> shortNames);
}
findByShortName 方法调用的结果是我预期的元素列表,除了每个元素都有空值 shortName(其他字段正确)。
我直接在 MySQL 数据库上执行完全相同的查询,它返回 short_name 列的正确值。
我应该怎么做才能在我的投影类上拥有一个有效的 shortName?
【问题讨论】:
标签: java mysql spring-boot jpa