为了账号安全,请及时绑定邮箱和手机立即绑定

投影对象上字符串字段的空值

投影对象上字符串字段的空值

POPMUISE 2021-12-30 16:23:42
在 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()


查看完整回答
反对 回复 2021-12-30
  • 1 回答
  • 0 关注
  • 206 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号