在 Spring Boot 应用程序中,我有一个复合 id 定义如下的类:@Embeddablepublic 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}(简化的)类定义是:@Entitypublic 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?
1 回答
狐的传说
TA贡献1804条经验 获得超3个赞
投影似乎不适用于本机查询,下划线的查询更精确。要获取其中有下划线的字段,请在 get 方法中使用下划线。所以而不是使用getShortName()使用getShort_name()
添加回答
举报
0/150
提交
取消
