Mapping entity relationships
Database relationships are one of the most important concepts in relational databases. From an object-oriented programming perspective, database relationships are seen as an association relationship. An object may reference an instance of another class, or a list of instances of this class, and this is called association, or the has-a relationship in OOP, which maps directly to the concept of relational databases.
JPA provides a complete framework for mapping and using database relationships using object-oriented approaches. The rule of thumb is very easy: when an entity class references an instance (or a list of instances) of another entity class, this should be mapped to a database relationship. Four types of relationship exist in JPA:
- One-to-one
- One-to-many
- Many-to-many
- Many-to-one
Before moving on, let's refresh our concepts about these relationships. From an object-oriented perspective, when an entity references another singular entity, this is a to-one relationship...