Top Description Methods
jakarta.persistence

public @Interface ForeignKey

extends Annotation
Annotations
@Target
@Retention:RUNTIME
Static Imports
java.lang.annotation.RetentionPolicy.RUNTIME, jakarta.persistence.ConstraintMode.CONSTRAINT

Specifies the handling of foreign key constraints when schema generation is in effect. If this annotation is not specified, a default foreign key strategy is selected by the persistence provider.

The ConstraintMode value is used to specify whether foreign key constraints should be generated.

The syntax used in the foreignKeyDefinition element should follow the SQL syntax used by the target database for foreign key constraint creation. For example, it might be similar to the following:

FOREIGN KEY ( <COLUMN expression> {, <COLUMN expression>} ... ) REFERENCES <TABLE identifier> [ (<COLUMN expression> {, <COLUMN expression>} ... ) ] [ ON UPDATE <referential action> ] [ ON DELETE <referential action> ]
FOREIGN KEY ( <COLUMN expression> {, <COLUMN expression>} ... )
    REFERENCES <TABLE identifier> [
        (<COLUMN expression> {, <COLUMN expression>} ... ) ]
    [ ON UPDATE <referential action> ]
    [ ON DELETE <referential action> ]

When the ConstraintMode value is CONSTRAINT, but the foreignKeyDefinition element is not specified, the provider will generate foreign key constraints whose update and delete actions it determines most appropriate for the join column(s) to which the foreign key annotation is applied.

Since
2.1
See Also
JoinColumn, JoinColumns, MapKeyJoinColumn, MapKeyJoinColumns, PrimaryKeyJoinColumn, JoinTable, CollectionTable, SecondaryTable, AssociationOverride

Method Summary

Modifier and TypeMethod and Description
public String
foreignKeyDefinition()

(Optional) The foreign key constraint definition.

public String
name()

(Optional) The name of the foreign key constraint.

public String
options()

(Optional) A SQL fragment appended to the generated DDL which creates this foreign key.

public ConstraintMode
value()

(Optional) Used to specify whether a foreign key constraint should be generated when schema generation is in effect.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

foreignKeyDefinitionback to summary
public String foreignKeyDefinition()

(Optional) The foreign key constraint definition.

nameback to summary
public String name()

(Optional) The name of the foreign key constraint.

Defaults to a provider-generated name.

optionsback to summary
public String options()

(Optional) A SQL fragment appended to the generated DDL which creates this foreign key. May not be used in conjunction with foreignKeyDefinition().

Since
3.2
valueback to summary
public ConstraintMode value()

(Optional) Used to specify whether a foreign key constraint should be generated when schema generation is in effect.

  • ConstraintMode#CONSTRAINT specifies that the persistence provider must generate a foreign key constraint. If the foreignKeyDefinition element is not specified, the provider will generate a constraint whose update and delete actions it determines most appropriate for the join column or columns to which the foreign key annotation is applied.
  • ConstraintMode#NO_CONSTRAINT specifies that no constraint should be generated.
  • ConstraintMode#PROVIDER_DEFAULT selects the default behavior of the provider, which may or may not result in generation of a constraint.