This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "users", indices = arrayOf(Index("full_name"), Index("phone_number"))) | |
data class User( | |
@PrimaryKey(autoGenerate = true) | |
var id: Int = 0, | |
@ColumnInfo(name = "full_name") | |
var name: String = "", | |
@ColumnInfo(name = "phone_number") | |
var phoneNumber: String = "" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "users", indices = arrayOf(Index("full_name"))) | |
data class User( | |
@PrimaryKey(autoGenerate = true) | |
var id: Int = 0, | |
@ColumnInfo(name = "full_name") | |
var name: String = "" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Query("SELECT users.*, city FROM users JOIN addresses ON users.address_id = addresses.id") | |
fun getUsersWithCity(): LiveData<List<UserWithAddress>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class UserWithAddress( | |
@Embedded | |
var user: User, | |
@ColumnInfo(name = "city") | |
var city: String? = "" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "users", , foreignKeys = arrayOf( | |
ForeignKey(entity = Address::class, parentColumns = arrayOf("id"), childColumns = arrayOf("address_id")) | |
) | |
data class User( | |
@PrimaryKey(autoGenerate = true) | |
var id: Int = 0, | |
var name: String = "", | |
@ColumnInfo(name = "address_id") | |
var address: Long? = null, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "addresses") | |
data class Address( | |
@PrimaryKey | |
var id: Long = 0, | |
var state: String = "", | |
var city: String = "" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Address( | |
var state: String = "", | |
var city: String = "" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "users") | |
data class User( | |
@PrimaryKey(autoGenerate = true) | |
var id: Int = 0, | |
var name: String = "", | |
@Embedded | |
var address: Address | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get instance | |
database = Room.databaseBuilder(context, AppDatabase::class.java, "app.db") | |
.build(); | |
// Example Usage | |
val userDao = database.userDao() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Database(entities = arrayOf(User::class), version = 1) | |
abstract class AppDatabase : RoomDatabase() { | |
abstract fun userDao(): UserDao | |
} |