Sobes.tech
Middle

What annotations do you know for DAO methods?

sobes.tech AI

Answer from AI

Here are the main annotations for methods in Room DAO:

  • @Query: Used to execute arbitrary SQL queries. Returns data or the number of rows affected/deleted.

    @Query("SELECT * FROM users WHERE id = :userId")
    fun getUserById(userId: Long): User?
    
  • @Insert: Marks a method for inserting one or more entities into the database. Can return Long (for a single inserted ID), LongArray (for multiple), or nothing (Unit).

    @Insert
    fun insertUser(user: User): Long
    
  • @Update: Marks a method for updating one or more entities in the database by their primary key. Returns the number of rows updated (Int).

    @Update
    fun updateUser(user: User): Int
    
  • @Delete: Marks a method for deleting one or more entities from the database by their primary key. Returns the number of rows deleted (Int).

    @Delete
    fun deleteUser(user: User): Int
    
  • @Transaction: Used to execute multiple DAO operations in a single transaction. Ensures atomicity: either all operations succeed or none are executed.

    @Transaction
    @Query("SELECT * FROM playlists WHERE creatorId = :creatorId")
    fun getPlaylistsWithSongsByCreator(creatorId: Long): List<PlaylistWithSongs>
    
What annotations do you know for DAO methods? — Android - sobes.tech