🗃️ JPA Q30 / 64

What is the difference between @Column and @JoinColumn?

AI-Powered Answer ✓ Answered

@Column and @JoinColumn are fundamental JPA annotations used to map entity fields to database columns. While both specify column details, their primary use cases and implications for relationships differ significantly.

@Column Annotation

The @Column annotation is used to specify the mapped column for a persistent property or field. It is primarily used for basic properties (e.g., String, Integer, Date, BigDecimal) that map directly to a single column in the entity's table. It allows you to customize various aspects of the database column, such as its name, length, nullability, uniqueness, precision, and scale.

java
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import java.math.BigDecimal;

@Entity
public class Product {
    @Id
    private Long id;

    @Column(name = "product_name", length = 100, nullable = false, unique = true)
    private String name;

    @Column(name = "price", precision = 10, scale = 2)
    private BigDecimal price;

    // Getters and setters
}

@JoinColumn Annotation

The @JoinColumn annotation is specifically used to map the foreign key column in a relationship between entities. It is typically applied to the owning side of a many-to-one or one-to-one relationship. It defines the column that references the primary key (or a unique key) of another entity. While it shares some attributes with @Column (like name, nullable, unique), its core purpose is to establish and manage associations between entities, thereby defining how relationships are persisted in the database.

java
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

@Entity
public class Order {
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = "customer_id", referencedColumnName = "id", nullable = false)
    private Customer customer;

    // Getters and setters
}

@Entity
public class Customer {
    @Id
    private Long id;
    private String name;
    // Getters and setters
}

Key Differences

Feature@Column@JoinColumn
PurposeMaps a basic entity property to a column in the same table.Maps a foreign key column to establish an entity relationship.
UsageFor scalar properties (String, int, Date, etc.) directly owned by the entity.For associations (ManyToOne, OneToOne) to reference another entity's primary key.
Relationship InvolvementNo direct role in entity relationships; for individual data points.Central to defining entity relationships (foreign key management).
Target ColumnRefers to a column within the entity's own table.Refers to a foreign key column that links to another entity's primary/unique key.
Key Attributesname, length, nullable, unique, precision, scale, columnDefinition, etc.name, referencedColumnName, nullable, unique, insertable, updatable, foreignKey, etc.

In summary, @Column is used for mapping simple fields to database columns that contain the entity's own data, whereas @JoinColumn is specifically for mapping foreign keys that link one entity to another, forming the basis of relationships between them.