I am trying to retrieve data from Repository using an SQL query but I am getting only values instead of keys and values in JSON response when I am trying to retrieve it.
I have the following classes
Repository
@Repository
public interface ReportingDataRepository extends CrudRepository<Task, String> {
@Query(name = "task.getReportingData")
List<Task> getReportingData(String startDate, String endDate);
}
Controller
@RequestMapping(path = "/api/{startdate}/{enddate}")
public TaskScores getScoresDataByDate(@PathVariable("startdate") String startDate,
@PathVariable("enddate") String endDate) {
return new TaskScores((reportingDataRepository.getReportingData(startDate, endDate)));
}
Wrapper class within the controller
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TaskScores {
List<Task> tasks;
}
Entity
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Data
@NamedNativeQuery(
name = "task.getReportingData",
query = "SELECT sys.id as id, sys.name as sys, sys.eventtype, workflow_agg.subprocess_id "
+ "FROM daily_agg_counts_system_score as sys_agg "
+ "join system_eventtype as sys on sys_agg.system_id = sys.id "
+ "join system_subprocess_mapping sm on sys_agg.system_id = sm.system_id "
+ "join daily_agg_workflow_completion_score workflow_agg on sm.subprocess_id = workflow_agg.subprocess_id "
+ "join sub_process sp on sp.id = sm.subprocess_id "
+ "join process as p on p.id = sys.process_id "
+ "join flow on flow.id = p.flow_id "
+ "join program on program.id = flow.program_id "
+ "where sys_agg.datepst BETWEEN ?1 AND ?2 "
+ "order by program.id asc, p.flow_id asc, sp.id asc, sys_agg.system_id asc, sys_agg.datepst asc "
+ "limit 1"
)
public class Task implements Serializable{
@Column(name = "id")
@Id
private String id;
@Column(name = "system")
private String sys;
@Column(name = "event_type")
private String eventType;
@ManyToOne(fetch = FetchType.LAZY)
private SubProcess subprocessId;
@Embeddable
class Score implements Serializable {
@JsonIgnore
String score;
}
}
I am using Spring Data JPA. When I am running this code I am getting the following JSON back
{
"tasks": [
[
1,
"oms",
"BE",
1
]
]
}
but I want it to be like actual json
{
"tasks": [
[
"id" : 1,
"sys" : "oms",
"eventType" : "BE",
"subprocessId" : 1
]
]
}
Any idea where I am going wrong?