Specifies the mapping for composite foreign keys. This annotation groups
JoinColumn annotations for the same relationship.
Each JoinColumn annotation must explicit specify both
name and referencedColumnName.
Since @JoinColumn is a repeatable annotation, it's not usually
necessary to specify @JoinColumns explicitly:
@ManyToOne
@JoinColumn(name = "ADDR_ID", referencedColumnName = "ID")
@JoinColumn(name = "ADDR_ZIP", referencedColumnName = "ZIP")
public Address getAddress() { return address; }
However, @JoinColumns is useful for controlling generation of
composite foreign key constraints:
@ManyToOne
@JoinColumns(
value = {@JoinColumn(name = "ADDR_ID", referencedColumnName = "ID"),
@JoinColumn(name = "ADDR_ZIP", referencedColumnName = "ZIP")},
foreignKey = @ForeignKey(name = "PERSON_ADDRESS_FK"))
public Address getAddress() { return address; }
- Since:
- 1.0
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescription(Optional) Controls generation of the foreign key constraint on these join columns when table generation is in effect.(Optional) The join columns that map the relationship.
-
Element Details
-
value
JoinColumn[] value(Optional) The join columns that map the relationship.If no
@JoinColumns are specified, the join columns are inferred according to the relationship mapping defaults, exactly as if the@JoinColumnsannotation was missing. This allows theforeignKey()to be specified when the join columns are defaulted.- Default:
{}
-
foreignKey
ForeignKey foreignKey(Optional) Controls generation of the foreign key constraint on these join columns when table generation is in effect.- If both this element and a
foreignKeyelement of one of theJoinColumnannotations are specified, the behavior is undefined. - If no
ForeignKeyannotation is specified in either location, a default foreign key strategy is selected by the persistence provider.
- Since:
- 2.1
- See Also:
- Default:
@jakarta.persistence.ForeignKey(PROVIDER_DEFAULT)
- If both this element and a
-