我有一个包含 3 条记录的表 form_header
表中还有更多字段决定不在帖子中添加,因为大多数字段无关紧要。我创建了一个类/实体来获取 sql 中每个状态的不同计数。
@Entity()
@Table(name = "Form_Header")
@SqlResultSetMapping(name = "myMapping",
entities = {@EntityResult(
entityClass = FormSummary.class,
fields = {@FieldResult(name = "status", column = "status"),
@FieldResult(name = "id", column = "header_id")})})
public class FormSummary {
@Id()
private Long id;
private String status;
<getter and setter>
与实体经理
List<FormSummary> results = entityManager.createNativeQuery("select DISTINCT(status), COUNT(header_id) as header_id from Form_Header where is_deleted = 0 group by status order by status", "myMapping").getResultList();
for (FormSummary x : results) {
System.out.println("ABC " + x.getId());
System.out.println("ABC " + x.getStatus());
}
问题是系统输出显示这个
而不是这个
status header_id
APPROVE 1
DRAFT 1
SUBMITTED 1
更奇怪的是,如果我在表中添加一条具有相同状态的额外记录
我将在我的 jpa 中获得正确的数据
我的代码中是否缺少某些内容或 SqlResultSetMapping 可能存在错误?