Problem: After a long research how I can serialize data from a database with two tables I have found that tutorial. But I need to get the data out of the database and I do not know how to get connect the data to the database. I have found only non-relational samples with one table.
Question: Does someone have a sample for the DAO class to get the data for the characteristics out of the database?
JSON structure needed:
[
{
"id":1,
"name":"CNC",
"beschreibung":"Metallverarbeitung",
"characteristics":[
"id_characteristic":1,
"id_maschine":2,
"name":"size",
"description":"length of maschine",
"type":1
]
}
]
current JSON structure:
[
{
"id":1,
"name":"CNC",
"beschreibung":"Metallverarbeitung",
"characteristics":[
]
},
...
]
DAO method (until now, does not fill the characteristics Array):
@Override
public List<Maschine> list() {
String selectMaschines = "SELECT * FROM maschine";
List<Maschine> listMaschine = jdbcTemplate.query(selectMaschines, new RowMapper<Maschine>() {
@Override
public Maschine mapRow(ResultSet rs, int rowNum) throws SQLException {
Maschine aMaschine = new Maschine();
aMaschine.setId(rs.getInt("Maschine_id"));
aMaschine.setName(rs.getString("name"));
aMaschine.setBeschreibung(rs.getString("beschreibung"));
return aMaschine;
}
});
return listMaschine;
}
Table structure:
Maschine table:
maschine_id || name || description
Characteristic table:
id_characteristic || id_maschine || name || description || type