@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
