Java 投影对象上字符串字段的空值
在Spring引导应用程序中,我有一个复合id的类,定义如下:Java 投影对象上字符串字段的空值,java,mysql,spring-boot,jpa,Java,Mysql,Spring Boot,Jpa,在Spring引导应用程序中,我有一个复合id的类,定义如下: @Embeddable public class StatisticId implements Serializable { private static final long serialVersionUID = 1L; @Column(length = 255, nullable = false) private String shortName; @Enumerated(EnumType.
@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);
}
public interface statistics repository扩展了crudepository{
@查询(value=“从统计中选择短名称、月份、年份、销售额,其中短名称in=?1”,nativeQuery=true)
Iterable findByShortName(集合短名称);
}
findByShortName方法调用的结果将产生我所期望的元素列表,但每个元素都有空值的shortName(其他字段是正确的)
我直接在MySQL数据库上执行完全相同的查询,它返回short\u name列的正确值
我应该怎么做才能在我的projection类中有一个有效的短名称呢?Projections似乎不能很好地处理本机查询,更准确地说是带下划线的查询。要获取包含下划线的字段,请在get方法中使用下划线。因此,与其使用
getShortName()
不如使用getShortName()
投影似乎不能很好地处理本机查询,更准确地说,是带下划线的查询。要获取包含下划线的字段,请在get方法中使用下划线。因此,不要使用getShortName()
而是使用getShort\u name()