我在数据库中有两个表:
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 。
现在我为这两个表制作了一些实体对象。但是我如何实现Student和Grade之间的关系?
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.
...
或者其他的东西?
第二个代码片段还要求我获取从数据库派生的成绩实体对象。