I'm working on a Spring Boot application and I have encountered the following challenge when trying to find the SUM. I have tried using JPQL and JPA syntax, but I'm still getting a null pointer exception.
@Getter
@Setter
@NoArgsConstructor
@Entity
public class ReportData extends Auditable {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long reportId;
private Integer tests;
}
@Repository
public interface reportRepository extends JpaRepository <ReportData, Long> {
// or "SELECT SUM(m.tests) FROM ReportData m"
@Query(value = "SELECT SUM(tests) FROM ReportData", nativeQuery = true)
int totalTest ();
}
@GetMapping(value = "/reports")
public String statisticsPage (Model model){
model.addAttribute("tests", reportRepository.totalTest());
return ("statistics");
}