如何将表关系实现到实体对象?

时间:2013-06-01 12:59:55

标签: model-view-controller entity-relationship

我在数据库中有两个表:

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.
    ...

还是什么?

第二个代码片段要求我也获取从数据库派生的成绩实体对象。

0 个答案:

没有答案