📕
Knowledge
  • Knowledge Repository
  • Java
    • Intro
    • Persistence
      • Java Persistence API (JPA)
        • Entity Manager
        • Transaction Management
        • Relationship Mapping
      • Java Transaction API (JTA)
    • Resources
  • Angular
    • Intro
    • Overview
    • CLI Commands
    • Directives
  • Tools
    • IDEs
      • IntelliJ
        • Shortcuts
  • Book Notes
    • Intro
    • Java A Beginner's Guide
      • Chapter 1. Java Fundamentals
      • Chapter 2. Data Types and Operators
      • Chapter 3. Program Control Statements
      • Chapter 4. Introducing Classes, Objects, and Methods
      • Chapter 5. More Data Types & Operators
      • Chapter 6. Closer Look At Methods and Classes
      • Chapter 7. Inheritance
      • Chapter 8. Packages & Interfaces
      • Chapter 9. Exception Handling
      • Chapter 10. Using I/O
    • Data Pipelines With Airflow
      • Chapter 1. Meet Airflow
      • Chapter 2. Anatomy Of a DAG
  • Course Notes
    • Intro
    • Spring: TDD With JUnit
Powered by GitBook
On this page
  • Relationship Types
  • One-To-Many
  • Many-To-One
  • One-To-One
  • Many-To-Many
  • Join Types

Was this helpful?

  1. Java
  2. Persistence
  3. Java Persistence API (JPA)

Relationship Mapping

Entities in a database table will often have a relationship to an entity in another database table. The most common example of this is the use of a foreign key which can be used to join data across two or more tables.

There are four main relationship (cardinalities) types that can be used to map data across entities, each with a dedicated annotation:

  1. One-to-Many

  2. Many-to-One (@ManyToOne)

  3. One-to-One

  4. Many-to-Many

Relationship Types

One-To-Many

A one-to-many relationship exists when one row in table A may be linked with many rows in table B, but one row in table B is linked to only one row in table A.

For example an online shopping basket can have many items, so here we have a one-to-many mapping.

Many-To-One

In this relationship, many entities can relate to a single entity. For example, a Company entity will have many Employee entities associated to it.

The @JoinColumn annotation in the code below instructs how the relationship is to be mapped. In this case, join the employee on the company_id and store this in the company_id column.

@Entity
public class Employee {

//id and other fields

@ManyToOne
@JoinColumn(name = "company_id")
private Company company;

//constructor

//getters and setters

}

One-To-One

Notes to be updated

Many-To-Many

In this relationship two entities can reference each other, but also other entities too.

For example, a company can associate with many different freelance workers, and freelancers can associate with many different companies.

Join Types

There are two ways to join the information across tables, using @JoinColumn and @JoinTable.

When using @JoinColumn, typically in many-to-one relationships, the two columns across two tables will store the same reference (id). For example, the id column on Company table will store in the company_id column on the Employee table, thus linking the employee record with the company.

When using @JoinTable, typically in many-to-many relationships, a third table is generated that stores the linking id's. For example:

id

company_id

employee_id

01

00001

00056

02

00001

00045

03

00001

00123

04

00002

001323

When the data is required this table will be referenced to join the correct records.

Which one should I use? This will depend on the use case. Please note that JoinColumn is typically more performant as a third table is not required to process data. So if JoinColumn is sufficient for the use case then this should be used.

PreviousTransaction ManagementNextJava Transaction API (JTA)

Last updated 4 years ago

Was this helpful?