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 = "entity_sample", | |
foreignKeys = [ | |
(ForeignKey(entity = User::class, parentColumns = ["id"], childColumns = ["userId"], onDelete = CASCADE)), | |
(ForeignKey(entity = Post::class, parentColumns = ["id"], childColumns = ["postId"], onDelete = CASCADE))], | |
indices = [ | |
(Index(value = ["postId"], name = "EntitySamplePostIndex")), | |
(Index(value = ["userId", "postId"], name = "EntitySampleUserPostIndex", unique = true)) | |
] | |
) | |
data class EntitySample( |
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
@Module | |
public class DbModule { | |
@Singleton | |
@Provides | |
public SampleDb provideDb(Application application) { | |
return Room.databaseBuilder(application, SampleDb.class, "sample.db").build(); | |
} | |
@Singleton |
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
// Define table name foreign keys and indices. Second index defines a unique pair. | |
@Entity( | |
tableName = "entity_sample", | |
foreignKeys = { | |
@ForeignKey(entity = User.class, parentColumns = "id", childColumns = "user_id", onDelete = ForeignKey.CASCADE), | |
@ForeignKey(entity = Post.class, parentColumns = "id", childColumns = "post_id", onDelete = ForeignKey.CASCADE) | |
}, | |
indices = { | |
@Index(value = "post_id", name = "PostIndex"), | |
@Index(value = {"user_id", "post_id"}, name = "UserPostIndex", unique = true) |
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
public class Converters { | |
// Date type conversion methods | |
@TypeConverter | |
public Date fromTimestamp(Long value) { | |
return value == null ? null : new Date(value); | |
} | |
@TypeConverter | |
public Long dateToTimestamp(Date date) { | |
if (date == 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
// Define tables via entity classes, set database version | |
@Database(version = 1, entities = {User.class, Post.class}) | |
// Add type converters | |
@TypeConverters(Converters.class) | |
abstract public class SampleDb extends RoomDatabase { | |
private static volatile SampleDb INSTANCE; | |
public static SampleDb getInstance(Context context) { | |
if (INSTANCE == null) { | |
synchronized (SampleDb.class) { |