Module is.codion.framework.domain
Interface ForeignKey
- All Superinterfaces:
Attribute<Entity>,ForeignKeyConditions
An
Attribute representing a foreign key relation.
Foreign keys establish relationships between entities, allowing navigation from one entity to related entities. They represent database foreign key constraints and enable automatic loading of referenced entities based on reference depth configuration.
Foreign keys inherit from ForeignKeyConditions to provide condition creation methods:
interface Album {
EntityType TYPE = DOMAIN.entityType("chinook.album");
Column<Long> ID = TYPE.longColumn("id");
Column<String> TITLE = TYPE.stringColumn("title");
Column<Long> ARTIST_ID = TYPE.longColumn("artist_id");
// Single-column foreign key
ForeignKey ARTIST_FK = TYPE.foreignKey("artist_fk", ARTIST_ID, Artist.ID);
}
EntityDefinition album() {
return Album.TYPE.as()
.attributes(
Album.ID.as()
.primaryKey(),
Album.TITLE.as()
.column()
.caption("Title"),
Album.ARTIST_ID.as()
.column(),
Album.ARTIST_FK.as()
.foreignKey()
.caption("Artist")
.referenceDepth(1)) // Load the artist automatically (1 is the default)
.build();
}
void albums(EntityConnection connection) {
// Foreign key navigation and usage
List<Entity> albums = connection.select(all(Album.TYPE));
for (Entity album : albums) {
// Direct foreign key entity access (loaded automatically with reference depth)
Entity artist = album.get(Album.ARTIST_FK);
if (artist != null) {
System.out.println("Artist: " + artist.get(Artist.NAME));
}
// Or use entity() to get the entity even if not loaded
Entity artistEntity = album.entity(Album.ARTIST_FK);
if (artistEntity != null) {
Long artistId = artistEntity.get(Artist.ID); // Always available
}
}
// Query conditions using foreign keys
Entity acdc = connection.selectSingle(Artist.NAME.equalTo("AC/DC"));
List<Entity> acdcAlbums = connection.select(
Album.ARTIST_FK.equalTo(acdc));
List<Entity> albumsByTheArtists = connection.select(
Album.ARTIST_FK.in(connection.select(Artist.NAME.like("The %"))));
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceProvidesForeignKeyDefinition.Builderinstances.static interfaceRepresents a foreign key reference between columns.Nested classes/interfaces inherited from interface is.codion.framework.domain.entity.attribute.Attribute
Attribute.AttributeDefiner<T>, Attribute.DataType<T> -
Method Summary
Modifier and TypeMethodDescriptionas()static ForeignKeyforeignKey(EntityType entityType, String name, List<ForeignKey.Reference<?>> references) Creates a newForeignKeybased on the given entityType and references.<T> ForeignKey.Reference<T> static <T> ForeignKey.Reference<T> Returns a newForeignKey.Referencebased on the given columns.Methods inherited from interface is.codion.framework.domain.entity.attribute.Attribute
as, entityType, name, typeMethods inherited from interface is.codion.framework.domain.entity.condition.ForeignKeyConditions
equalTo, in, in, isNotNull, isNull, notEqualTo, notIn, notIn
-
Method Details
-
as
- Specified by:
asin interfaceAttribute<Entity>- Returns:
- a
ForeignKey.ForeignKeyDefinerfor this foreign key
-
referencedType
EntityType referencedType()- Returns:
- the entity type referenced by this foreign key
-
references
List<ForeignKey.Reference<?>> references()- Returns:
- the
ForeignKey.References that comprise this key
-
reference
- Type Parameters:
T- the column type- Parameters:
column- the column- Returns:
- the reference that is based on the given column
-
reference
Returns a newForeignKey.Referencebased on the given columns.- Type Parameters:
T- the column type- Parameters:
column- the local columnforeign- the referenced foreign column- Returns:
- a new
ForeignKey.Referencebased on the given columns
-
foreignKey
static ForeignKey foreignKey(EntityType entityType, String name, List<ForeignKey.Reference<?>> references) Creates a newForeignKeybased on the given entityType and references.- Parameters:
entityType- the entityType owning this foreign keyname- the attribute namereferences- the references- Returns:
- a new
ForeignKey - See Also:
-