MapColumn
@Target(allowedTargets = [AnnotationTarget.TYPE])
@Retention(value = AnnotationRetention.BINARY)
public annotation MapColumn
Declares which column is used to build a map or multimap return value in a Dao query function.
This annotation is required when the key or value of a Map (or nested map) is a single column of one of the built-in types (primitives, enums, String, ByteArray) or a type with a ColumnTypeConverter (e.g. Date, UUID, etc).
The use of this annotation provides clarity on which column should be used in retrieving information required by the DAO function return type.
Example:
@Query("SELECT * FROM Artist JOIN Song ON Artist.artistName = Song.artist")
suspend fun getArtistNameToSongNames():
Map<@MapColumn("artistName") String, @MapColumn("songName") List<String>>
@Query(
"""
SELECT *, COUNT(mSongId) as songCount
FROM Artist JOIN Song ON Artist.artistName = Song.artist GROUP BY artistName
"""
)
suspend fun getArtistAndSongCounts():
Map<Artist, @MapColumn(columnName = "songCount") Integer>
Column(s) specified in this @MapColumn's annotation values must be present in the query result.
Summary
Public methods |
|
|---|---|
final @NonNull String |
The name of the column to be used for the map's key or value. |
final @NonNull String |
The name of the table or alias to be used for the map's column. |
Public constructors
Public methods
getColumnName
public final @NonNull String getColumnName()
The name of the column to be used for the map's key or value.
getTableName
public final @NonNull String getTableName()
The name of the table or alias to be used for the map's column.
Providing this value is optional. Useful for disambiguating between duplicate column names. For example, consider the following query: SELECT * FROM Artist AS a JOIN Song AS s ON a.id == s.artistId, then the @MapColumn usage for a return type Map<String, List<Song>> would be Map<@MapColumn(columnName = "id", tableName = "a") String, List<Song>>.