Conditions in Codion are composable, strongly-typed query filters used to construct WHERE or HAVING clauses for select, update, and count operations. They are typically created via domain attributes (like Column or ForeignKey), and can be freely combined using logical operators like AND
and OR
.
The Chinook domain model is used in the examples below.
1. Condition
- Condition
-
Represents a query condition and contains factory methods for creating
Condition
instances. - ColumnCondition
-
Represents a column based
Condition
. - ForeignKeyCondition
-
Represents a foreign key based
Condition
.
Note
|
Column and ForeignKey implement their respective condition factory interfaces (ColumnCondition.Factory and ForeignKeyCondition.Factory), so you can create Condition instances directly from them using fluent methods like .equalTo() , .isNull() , .in() , etc.
|
Condition allArtistsCondition =
Condition.all(Artist.TYPE);
List<Entity> artists =
connection.select(allArtistsCondition);
Condition liveAlbums =
Album.TITLE.likeIgnoreCase("%Live%");
List<Entity> albums =
connection.select(liveAlbums);
Entity metallica =
connection.selectSingle(
Artist.NAME.equalTo("Metallica"));
Condition albums =
Album.ARTIST_FK.equalTo(metallica);
- CustomCondition
-
A CustomCondition can be used when your logic can’t be expressed through column-based or foreign-key-based conditions — for example, when writing native SQL fragments or using DB-specific syntax.
List<Long> classicalPlaylistIds =
List.of(42L, 43L);
Condition noneClassical =
Track.NOT_IN_PLAYLIST.get(
Playlist.ID, classicalPlaylistIds);
List<Entity> tracks =
connection.select(noneClassical);
- Condition.Combination
-
Allows you to combine multiple conditions using logical
AND
/OR
operators. Conditions can be nested to build expressive and complex query logic.
Condition liveMetallicaAlbums =
Condition.and(liveAlbums, metallicaAlbums);
List<Entity> albums =
connection.select(liveMetallicaAlbums);
2. Select, Update, Count
The EntityConnection.Select
, EntityConnection.Update
, and EntityConnection.Count
classes each provide a .where(Condition)
factory method returning a builder object for further configuration.
Select.where returns a Select.Builder.
Update.where returns a Update.Builder.
Count.where returns a Count.Builder
2.1. Select
- EntityConnection.Select
-
Represents a
WHERE
condition as well as extended configuration specifically for selecting, such as orderBy, limit, offset and referenceDepth.
Select selectLiveMetallicaAlbums =
Select.where(liveMetallicaAlbums)
.orderBy(OrderBy.descending(Album.NUMBER_OF_TRACKS))
.build();
List<Entity> albums =
connection.select(selectLiveMetallicaAlbums);
2.2. Update
- EntityConnection.Update
-
Represents a
WHERE
condition as well as the columns and values for updating one or more entities.
Update removeLiveMetallicaAlbumCovers =
Update.where(liveMetallicaAlbums)
.set(Album.COVER, null)
.build();
int updateCount =
connection.update(removeLiveMetallicaAlbumCovers);
2.3. Count
- EntityConnection.Count
-
Represents a
WHERE
condition specifically for counting records.
Count countAlbumsWithCover =
Count.where(Album.COVER.isNotNull());
int count = connection.count(countAlbumsWithCover);