我不确定你确切想要什么,但如果你想让嵌套的Java对象(实体)对应于表格,请继续阅读:
由于表格包含座位包含轮次包含菜名,因此您从最内部的实体(菜品)开始:
Public class Dish{
private int id; // an id
private String dish_name;
// getters and setters
}
你的Round
包含了Dish
Public class Round{
private int id; // an id
private List<Dish> dishes;
// getters and setters
}
Public class Seat{
private int id; // an id
private List<Round> rounds;
// getters and setters
}
Public class Table{
private int id; //
private List<Seat> seats;
// getters and setters
}
class Table
List<Seat> seats = ArrayList<>();
class Seat
List<Round> rounds = ArrayList<>();
class Round
List<Seat> seats = ArrayList<>();
class Dish
public class Table {
public final int id;
public final List<Seat> seats = ArrayList<>();
public Table(int id) {
this.id = id;
}
}
为构建唯一实体对象,您需要将键映射到实体对象。
Map<Integer, Table> tablesById = new HashMap<>();
Map<Integer, Seat> seatsById = new HashMap<>();
Map<Integer, Round> roundsById = new HashMap<>();
Map<String, Dish> dishesByName = new HashMap<>();
Table table = new Table(resultSet.getInt("Table"));
table = tablesById.putIfAbsent(table.id, table);
// Now table is a maybe already existing table of the map.
Seat seat = new Table(resultSet.getInt("Seat"));
seat = seatsById.putIfAbsent(seat.id, seat);
table.seats.add(seat);
...
putIfAbsent
方法自 Java 8 开始存在。在早期的 Java 中,您可以使用 get
方法,只有当它不存在时才能 put
一个新表。