Turning a class into a table.

Many modern businesses use programming languages like Java or C# to build applications and use a relational database to store the needed data. There are other options, but object-oriented technologies and relational databases are the norm. To store classes in a relational database, it is necessary to to a class to table conversion.

The first step when doing this conversion is to look at the attributes of the class. All attributes are mapped to zero or more columns in a database. An example of an attribute that won’t be stored in a database is your age (if you are already storing your date of birth your age can be calculated using that) or the average grade of a student (if you have the grade of all the courses, the average can be calculated without the need to store it). If an attribute of an object is another object, there will be an association between these two objects and the second object will have its attributes mapped. This is done recursively until you obtain an attribute that maps to zero or more columns.

Image source

However, mapping a class to a table isn’t always so easy. Inheritance is something that can complicate mapping quite a bit. How do you map correctly inherited attributes? When dealing with inheritance there are multiple techniques you can use to successfully map it.

The first approach is to map the whole hierarchy to a table. You will probably end up with huge tables, but it works. The next approach is for each concrete class to have its own table. For example, if you have three classes, user, student and teacher, and both student and teacher inherit from user, you will create two tables for student and teacher. Each of the tables will have both the attributes of that class and the attributes of the user class, in the previous approach, it would all have been just a big table. And the last approach I will talk about in this post is mapping each class to one table. Using the previous example, there will be three tables instead of two, one for each class. The way student and teacher will inherit the attributes is by having a foreign key that points to the user table.

Another thing that can be difficult to map are relationships between classes. A few key points are that if a class has a getter method, that class probably has a one-to-one or a one-to-many relationship with the class the getter is getting from. For example, if you have an Employee class that has an attribute of type Department (Department is another class), the getter method that returns the department will always return only one value. However, different employees may return the same department from the getter method, this means that there is a one-to-many relationship between Employee and Department. If every employee returned a different department, the relationship would be one-to-one. Another key for defining relationships are arrays. If a class has an array whose type is another class, that would mean that the multiplicity of that class in that relationship is 0..* or 0..1. Whether this relationship is one-to-many or many-to-many, it all depends on the other class and how it is related to the original class.

I have never worked with non-relational databases, but, from what I gathered, it’s still possible to do the mapping. The most basic case is simple, each field name maps to one object attribute name. Next, there are embedded values, which are used for collections, the @Embedded annotation. For relationships, annotations are also used (@OneToOne, @OneToMany, @ManyToMany, etc.).

Image source

Most modern businesses use object-oriented technologies together with relational databases. Given that this is the norm, mapping classes to tables becomes an important part for many businesses. It may turn out to be more difficult depending on your system, but the mapping can be done.

Leave a comment

Design a site like this with WordPress.com
Get started