Mapper is a library that allow the object-relational mapping from Relational Databases to Java objects.
This library was developed as part of an assigment in the subject Databases II from the Computer Engineering Degree in the University of Santiago de Compostela. Specifically, it consisted in the development of a social network which required the usage of SQL databases.
The library provides the user with an interface which relies on the usage of the annotations available in Java. These can be classified in two categories:
- MapperTable: class level annotation that points the library which is the database's table from where the data will be mapped.
- MapperColumn: attribute level annotation that points the library which is the database's column from where the data will be obtained.
Via these annotations, the library is able to parse basic SQL tables along with their properties, such as primary keys, or default values. Following up, an example shows a Java class Student which maps the Students table.
@MapperTable(nombre="students")
public class Student {
@MapperColumn(columna="name")
private String name;
@MapperColumn(pkey=true)
private String dni;
@MapperColumn
private Date birthday;
@MapperColumn(fkey="teacher:id", targetClass="Teacher.class")
private Teacher teacher;
}
To map an already defined Java class using annotations, the user will need to rely on the available Mapper classes in the library. Within these, a parent class is divided into several subclasses, each one specializing in an specific kind of tasks:
- InsertionMapper: performs the insertion of an object in the database.
- QueryMapper: performs queries over the database, allowing the recovery of every present information in the shape of a mapped class' instance.
- DeleteMapper: performs the deletion of the specified object in the database.
- UpdateMapper: performs the update of the specified object's data in the database.
Some of the benefits that the Mapper can provide to a software project are:
- Disengage the connection with the database through the usage of the mapper as an intermediary in the sharing of data.
- Increase of the security, as the mapper with take care of preventing common security flaws in software such as SQL injection.
- Ease of executing SQL sentences, due to the abstraction that the mapper provides over the connection with the database.
- The possibility of using non-atomic classes as foreign keys (namely, using a Java class as a foreign key instead of a single attribute); in addition, the mapper is able to extract from the database all the information about the referenced class as an object, not only its primary key, but also its other data regardless of its type, providing the user with the appropiate abstraction.