Course – LSD – NPI (cat=Persistence)
announcement - icon

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn the difference between @JoinColumn and @PrimaryKeyJoinColumn in Java Persistence API (JPA).

JPA lets applications access data in relational databases. It lets us define how data is stored in a database and how it can be accessed by applications.

Java objects are mapped to database tables with JPA annotations. There are several JPA annotations, but we’ll focus on @JoinColumn and @PrimaryKeyJoinColumn.

2. @JoinColumn Annotation

The @JoinColumn annotation defines a column that will join two entities. It defines the foreign key column of an entity and its associated primary key field.

This annotation allows us to create relationships between entities, saving and accessing data quickly. Usually, it’s used with the @ManyToOne or @OneToOne annotations to define an association.

The example below shows how to use the @JoinColumn annotation:

@Entity
public class Email {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String address;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id", referencedColumnName = "id")
    private OfficialEmployee employee;
   
    //getters and setters
}
@Entity
public class OfficialEmployee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    private List<Email> emails;

    //getters and setters
}
In the example above, the Email and OfficialEmployee entities have a many-to-one relationship. It’s the @JoinColumn that indicates the join column between the two entities which is “employee_id”.

 

The name parameter specifies the foreign key column in employees, while referencedColumnName specifies the primary key column in OfficialEmployee table.

3. @PrimaryKeyJoinColumn Annotation

The @PrimaryKeyJoinColumn annotation specifies that an entity’s primary key is a foreign key to another entity. It maps a relationship between two entities by making their primary key columns foreign keys in the entity table.

The example below shows how to use the @PrimaryKeyJoinColumn annotation:

@Entity
@Table(name = "person")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Department department;

    // getters and setters
}

@Entity
@Table(name = "department")
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    // getters and setters
}

We have two entities in the example: Person and Department. The Person entity has a field called Department that references a Department entity using @OneToOne and @PrimaryKeyJoinColumn.

The Department table’s primary key will be the Person table’s foreign key. The Department entity has an @Id field that acts as the Department table’s primary key.

4. Differences Between @JoinColumn and @PrimaryKeyJoinColumn

The main difference between the JPA annotations @JoinColumn and @PrimaryKeyJoinColumn lies in their treatment of the relationship between the entities.

@JoinColumn refers to a column in a table that holds a foreign key to another table. This annotation is employed when a foreign key in one table refers to the primary key of another table.

Contrarily, @PrimaryKeyJoinColumn indicates that an association ought to be mapped by a foreign key column that corresponds to the associated entity’s primary key.

In short, the @JoinColumn maps a regular foreign key association. However, the @PrimaryKeyJoinColumn maps an association where the primary key of the associated entity is also used as a foreign key in the current entity.

It’s important to note that @PrimaryKeyJoinColumn is typically used in one-to-one mappings, but @JoinColumn can be used in one-to-one, one-to-many, many-to-one, and many-to-many mappings.

5. When to Use @JoinColumn vs. @PrimaryKeyJoinColumn

The way we model a relationship depends on the situation at hand. To clarify the mapping of a foreign key column, we can use @JoinColumn, especially when it’s not the primary key of the target entity.

When the foreign key column is the primary key of the target entity, we can use @PrimaryKeyJoinColumn. If an annotation isn’t specified, JPA employs a mapping strategy that relies on the entity and attribute names.

6. Conclusion

In this article, we looked at @JoinColumn and @PrimaryKeyJoinColumn, highlighting their distinctions. They join one entity to another in a one-to-one or many-to-one relationship.

As always, all source code is available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are closed on this article!