猿问

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

在 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?


POPMUISE
浏览 207回答 1
1回答

狐的传说

投影似乎不适用于本机查询,下划线的查询更精确。要获取其中有下划线的字段,请在 get 方法中使用下划线。所以而不是使用getShortName()使用getShort_name()
随时随地看视频慕课网APP

相关分类

Java
我要回答