我在数据库中有两个表:
Grade
+--------+--------------+-----------------+
| int id | varchar name | varchar teacher |
+--------+--------------+-----------------+
| 1 | CL-2A | E. Wright |
| 2 | CL-2B | B. Springsteen |
+--------+--------------+-----------------+
Student
+--------+------------------+-----------+
| int id | varchar name | int grade |
+--------+------------------+-----------+
| 1 | Michael Wallings | 1 |
| 2 | Rowen Tress | 1 |
| 3 | Alys Harverd | 2 |
| 4 | Jeff Bass | 1 |
| 5 | Harry Farewell | 2 |
+--------+------------------+-----------+
正如您可能已经看到的那样, Student.grade 使用外键引用 Grade.id 。
现在我为两个表创建了一些实体对象。但是如何实现学生和成绩之间的关系?
public class Student {
private int id;
private String name;
private int grade; // Keep the grade as an int.
...
或
public class Student {
private int id;
private String name;
private Grade grade; // Hold a reference to the corresponding entity object Grade.
...
还是什么?
第二个代码片段要求我也获取从数据库派生的成绩实体对象。