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

标签 java mysql spring-boot jpa

在 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

最佳答案

投影似乎不太适合 native 查询,更准确地说是下划线查询。要获取其中包含下划线的字段,请在 get 方法中使用下划线。因此,不要使用 getShortName(),而是使用 getShort_name()

关于java - 投影对象上字符串字段的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53146751/

相关文章:

php - 简单插入查询中的语法错误

java - 如何在 Spring Boot 中跟踪唯一的 Web 请求?

java.lang.OutOfMemoryError : PermGen space, 我在 Mac Os 上使用 tomcat

java - 如何为 Mac OSX 的 Java System.exec() 指定命令行来打开文档?

php - 使用 Laravel 加入表格并显示产品的最低价格

mysql - 没有主键和唯一键如何避免重复条目?

mysql - Hibernate Spring Boot中如何实现IS-A关系?

java - JWT 跳过登录页面的 URL

java - 无法使用 java 通过 selenium webdriver 点击 Facebook "setting"链接

JavaFX:动态更改舞台标题