Archive for the ‘many-to-many’ tag
O/R mapping – from Database Record to Entity Class
O/R mapping is a programming technique needed for converting data between relational database records and an object-oriented programming language.
Of course, there are plenty of O/R mapping tools, but I guess that a fool with a tool… So, lets look at O/R mapping then.
As you can see in the next picture there are subtle differences between the physical database model (database diagram) and the UML model (class diagram).
Aliases. Obviously there is a difference in naming. In the database there is a table called student while in code there is a class called Student. Similarly, the same holds true for their respective attributes. In the database a column is called first_name while in code it is named FirstName. Mappings needed here, however, are quite straightforward.
Type conversion. There also is the difference in data type. In the database you have something like varchar(25) or char(8) and in code it is in both cases a string. Mappings can be made, but not fully bidirectional. The problem is that in the database it has a length constraint whilst in code it has not.
Relations. In the class diagram the class Student has associations with both Mentor and Course classes. Besides the question how to represent the association, by id or collection, there is the problem of how to represent many-to-many associations. The UML model has 3 entities, while the database diagram has 4! The join table course_student is “lost” in the mapping.
In code this could be like this:
public class Student {
public string FirstName ;
public List<Course> Courses;
}
public class Course {
public string CourseName ;
public List<Student> Students;
}
public class Mentor {
public string MentorName ;
public List<Student> Students;
}















