Module is.codion.framework.domain
Interface ForeignKeyDefinition
- All Superinterfaces:
AttributeDefinition<Entity>
Represents a reference to another entity, typically but not necessarily based on a foreign key.
ForeignKeyDefinition configures how foreign key relationships behave, including reference depth for automatic loading, soft references for logical relationships, and attribute selection for referenced entities.
Foreign key definitions control the loading strategy and behavior of entity relationships:
// Reference depth 0, the invoice is not loaded
InvoiceLine.INVOICE_FK.as()
.foreignKey()
.referenceDepth(0);
// The default reference depth of 1 loads the artist
Album.ARTIST_FK.as()
.foreignKey();
// Foreign key with deeper reference depth, loading the album AND its artist.
// Note that only the foreign keys among the included attributes are populated,
// so ARTIST_FK must be included for the reference depth of 2 to reach the artist.
Track.ALBUM_FK.as()
.foreignKey()
.referenceDepth(2)
.include(Album.ARTIST_FK, Album.TITLE);
// Reference depth behavior examples:
// Reference depth 0: No automatic loading
List<Entity> tracks = connection.select(
Select.all(Track.TYPE)
.referenceDepth(0));
Entity track = tracks.get(0);
Entity album = track.get(Track.ALBUM_FK); // null - not loaded
Entity albumEntity = track.entity(Track.ALBUM_FK); // Contains only primary key
// Reference depth 2, as defined: Load the referenced entity and its references
List<Entity> tracksWithAlbums = connection.select(all(Track.TYPE));
Entity trackWithAlbum = tracksWithAlbums.get(0);
Entity loadedAlbum = trackWithAlbum.get(Track.ALBUM_FK); // Album is loaded
Entity artist = loadedAlbum.get(Album.ARTIST_FK); // Artist is also loaded
// WARNING: an unlimited reference depth, -1, with a cyclic foreign key reference
// in the data, causes infinite recursion, since no cycle detection is performed
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface is.codion.framework.domain.entity.attribute.AttributeDefinition
AttributeDefinition.DefaultValue<T> -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PropertyValue<Integer> Specifies the reference depth to use for foreign keys which do not specify one Value type: Integer Default value: 1Fields inherited from interface is.codion.framework.domain.entity.attribute.AttributeDefinition
DECIMAL_SEPARATOR, DESCRIPTION_RESOURCE_SUFFIX, FRACTION_DIGITS, GROUPING_SEPARATOR, LEXICAL_STRING_COMPARATOR, MNEMONIC_RESOURCE_SUFFIX, NUMBER_GROUPING, ROUNDING_MODE -
Method Summary
Modifier and TypeMethodDescriptionTheAttributethis definition is based on, should be unique within an Entity.booleanReturns true if the given foreign key reference column is read-only, as in, not updated when the foreign key value is set.Returns the reference depth specified for this foreign key, an emptyOptionalIntif none was specified, in which caseREFERENCE_DEPTHapplies.booleansoft()voidValidates the value of this attribute as found in the given entity.Methods inherited from interface is.codion.framework.domain.entity.attribute.AttributeDefinition
caption, comparator, dateTimeFormatter, dateTimePattern, derived, description, entityType, format, format, fractionDigits, hidden, mnemonic, roundingMode
-
Field Details
-
REFERENCE_DEPTH
Specifies the reference depth to use for foreign keys which do not specify one- Value type: Integer
- Default value: 1
- See Also:
-
-
Method Details
-
attribute
ForeignKey attribute()Description copied from interface:AttributeDefinitionTheAttributethis definition is based on, should be unique within an Entity. By default, theAttribute.name()serves as column name for database columns.- Specified by:
attributein interfaceAttributeDefinition<Entity>- Returns:
- the foreign key attribute this foreign key is based on.
-
referenceDepth
OptionalInt referenceDepth()Returns the reference depth specified for this foreign key, an emptyOptionalIntif none was specified, in which caseREFERENCE_DEPTHapplies.A specified depth of 0 means this foreign key is never populated automatically, not even when it is reached via another foreign key specifying a greater depth.
- Returns:
- the reference depth specified for this foreign key
- See Also:
-
soft
boolean soft()- Returns:
- true if this foreign key is not based on a physical (table) foreign key and should not prevent deletion
-
readOnly
Returns true if the given foreign key reference column is read-only, as in, not updated when the foreign key value is set.- Parameters:
referenceColumn- the reference column- Returns:
- true if the given foreign key reference column is read-only
-
references
List<ForeignKey.Reference<?>> references()- Returns:
- the
ForeignKey.References that comprise this foreign key
-
attributes
- Returns:
- the attributes to select when fetching entities referenced via this foreign key, an empty list in case of all attributes
-
validate
Validates the value of this attribute as found in the given entity.- Parameters:
entity- the entity containing the value to validatenullable- true if null values are allowed in this validation context, false if null should trigger a null validation exception- Throws:
AttributeValidationException- in case of an invalid value
-