liveBook · Manning

@JoinColumn

As explained in Java Persistence with Spring Data and Hibernate epub

@JoinColumn is a Jakarta Persistence annotation that specifies the relational column used to join an entity association.

@JoinColumn identifies the relational foreign-key column that connects an entity association to its target. In a Bid–Item many-to-one mapping, Hibernate defaults the column to ITEM_ID, but an explicit declaration can name the column and enforce NOT NULL with nullable = false. section 8.3.1

Many-to-one association

@ManyToOne(fetch = FetchType.LAZY)\n@JoinColumn(name = "ITEM_ID", nullable = false)\nprivate Item item;\n\nThe foreign key makes every Bid depend on an Item. The provider uses it to load the associated entity and manage the foreign-key value. The association can remain unidirectional; retrieving an Item’s bids can instead use a JPQL query. section 8.3.1

@JoinColumn also controls relationship shape and ownership in other mappings:

  • Use @OneToOne with unique = true to place a foreign-key reference such as SHIPPINGADDRESS_ID in the owning table and add a unique constraint. section 9.1.3
  • Use it on a unidirectional @OneToMany to place the association column in the child table; an @OrderColumn can add persistent list positions. section 9.2.2
  • In bidirectional mappings, the @ManyToOne side owns the foreign key, and schema generation relies on its @JoinColumn rather than the @OneToMany side. section 9.2.2
  • Prefer a join column over persistent list ordering when display order changes frequently, because maintaining indexes can trigger many SQL statements. section 9.2.2
Path: onetomany-embeddable/src/main/java/com/manning/javapersistence/ch09/
 onetomany/embeddable/Address.java
 
@Embeddable
public class Address {
    @NotNull
    @Column(nullable = false)
    private String street;
    @NotNull
    @Column(nullable = false, length = 5)
    private String zipcode;
    @NotNull
    @Column(nullable = false)
    private String city;
    @OneToMany
    @JoinColumn(
        name = "DELIVERY_ADDRESS_USER_ID",    #1
        nullable = false
    )
    private Set<Shipment> deliveries = new HashSet<>();
    //  . . .
}
#1 The first mapping strategy for this association is with a <kbd>@JoinColumn</kbd> named <kbd>DELIVERY_ADDRESS_USER_ID</kbd> (it would default to <kbd>DELIVERIES_ID</kbd>).

Shows a join column inside an embeddable association, including explicit naming and a non-null constraint. section 9.2.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains default naming, schema constraints, ownership, loading, duplicated mappings, and performance consequences across association types.
Examples
Uses Bid–Item, User–Address, ordered bids, and embedded Address–Shipment mappings with concrete Java annotations and SQL structures.
What it assumes
Assumes entity associations, foreign-key columns, and basic JPA mapping annotations.
What you can do after
Configure association mappings with explicit foreign-key names, nullability, uniqueness, ownership, and loading strategies.
How it connects
Connects foreign keys with entity associations, lazy loading, Bean Validation, join tables, embeddable classes, and persistent ordering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.