我是ormlite的新手,我想为某些字段设置默认值,例如
@DatabaseField(generatedId = true, canBeNull = false)
int id;
@DatabaseField(canBeNull = true)
String user_type;
@DatabaseField(canBeNull = true)
String username;
@DatabaseField(canBeNull = true)
String password;
@DatabaseField(canBeNull = true)
int id_color;
我必须为用户名字段设置默认值。我该怎么做呢?在此先感谢!!!!
答案 0 :(得分:11)
我必须为用户名字段设置默认值。我该怎么做?
我在ORMLite documentation上花了很多时间。你应该始终从那里开始。如果您转到文档索引并查找default value,则会显示defaultValue
注释中有@DatabaseField
字段。以下是javadocs for that field。
以下内容应该有效:
@DatabaseField(defaultValue = "unknownName", canBeNull = true)
String username;
答案 1 :(得分:1)
但是,当您必须将日期字段初始化为当前日期时,使用默认值是delecate。
因此,在您的域类的构造函数(让我们是Person)中,您应该按如下方式初始化此字段:
public Person(String user_type,...,Date enrollement){
this.user_type=user_type;
//....
// or Calendar.getInstance().getTime()
this.enrollement = (enrollement == null ) ? new Date() : enrollement;
}