gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:00:17 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com