pan*_*tus 1 java mysql jpa spring-boot
在 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
}
Run Code Online (Sandbox Code Playgroud)
(简化的)类定义是:
@Entity
public class Statistic implements Serializable {
private static final long serialVersionUID = 1L;
private BigDecimal sales;
@EmbeddedId
private StatisticId id;
// getters, setters, toString
}
Run Code Online (Sandbox Code Playgroud)
我想对这门课进行投影:
public interface StatisticProjection {
public String getShortName();
public Month getMonth();
public int getYear();
public BigDecimal getSales();
}
Run Code Online (Sandbox Code Playgroud)
并在以下存储库中使用它:
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);
}
Run Code Online (Sandbox Code Playgroud)
所述的结果findByShortName在元素列表的方法调用的结果,我所料,所不同的是每一个都具有零值SHORTNAME(其他字段是正确的)。
我直接在 MySQL 数据库上执行完全相同的查询,它返回一个short_name列的正确值。
我应该怎么做才能在我的投影类上拥有一个有效的shortName?
| 归档时间: |
|
| 查看次数: |
839 次 |
| 最近记录: |