I am having trouble mapping data from a database to my objects. Here is how my object are setup.
public class Report{
private int reportID;
private String reportName;
private List<Article> articles = new ArrayList<>();
// getters and setters
}
public class Article{
private int articleID;
private String articleName;
// getters and setters
}
The data from the database comes in the following format:
ReportID | Report Name | ArticleID | ArticleName
-----------------------------------------------------
1 Name1 1 Name1
1 Name1 2 Name2
1 Name1 3 Name3
2 Name2 1 Name1
2 Name2 2 Name2
3 Name3 1 Name1
As can be seen, each report can have 1-n articles. This is reflected in the pojo structure.
My issue is mapping the result set to the objects.
My DAO has the following method:
public List<Report> getReports(){
String SQL = "Select * FROM report_table";
List<Report> reports = jdbcTemplateObject.query(SQL, new RowMapper<Report>(){
@Override
public Report mapRow(ResultSet rs, int rowNum) throws SQLException{
// here I need to return each record
// but I need to add the articles to the record's list first!
return null;
}
});
return reports;
}
How can I go about mapping the articles to the reports?