如何注释Android房间实体内部的默认值? [英] How to annotate a default value inside a android room entity?
本文介绍了如何注释Android房间实体内部的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在查看 @ ColumnInfo 文档.
Room甚至提供默认值的注释吗?
Does Room even provide an annotation for default values?
我当前的解决方案是手动创建相应的表格...
My current solution would be to manually create the corresponding Table ...
CREATE TABLE MyTable (
...
MyDefaultValuedCol TEXT DEFAULT 'Default Value',
MyDefaultFlagCol INT DEFAULT 1
)
...并将房间"放在顶部.
... and put Room on top.
@Entity(tableName = "MyTable")
class MyClass {
...
public String MyDefaultValuedCol;
public boolean MyDefaultFlagCol;
}
推荐答案
随着房间持久性2.2.0的发布,在@ColumnInfo批注中添加了一个新属性,可用于指定列的默认值.参见文档.
With the release of room persistence 2.2.0, there's a new property added to @ColumnInfo annotation which can be used to specify the default value of a column. See documentation.
@Entity(tableName = "users")
data class User(
@PrimaryKey val id: Long,
@ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
@ColumnInfo(name = "last_modified", defaultValue = "CURRENT_TIMESTAMP" ) val lastModified: String
)
这篇关于如何注释Android房间实体内部的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文