Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW