schema active:标记一个实体处于活跃状态,活动实体有更新、删除和刷新方法
nameInDb:在数据库中使用的别名,默认使用的是实体的类名,
indexes:定义索引,可以跨越多个列
createInDb:标记创建数据库表(默认:true)
generateConstructors 自动创建全参构造方法(同时会生成一个无参构造方法)(默认:true)
generateGettersSetters 自动生成 getters and setters 方法(默认:true)
@Entity(
schema = “myschema”,
active = true,
nameInDb = “AWESOME_USERS”,
indexes = {
@Index(value = “name DESC”, unique = true)
},
createInDb = true,
generateConstructors = false,
generateGettersSetters = true
)
public class User {
…
}
@Id
对应数据表中的 Id 字段
@Entity
public class UserList {
@Id(autoincrement = true)//主键自动增长
private Long id;
@Index
使用@Index作为一个属性来创建一个索引,默认是使用字段名
@Entity
public class User {
@Id
private Long id;
@Index(unique = true)
private String name;
}
@OrderBy
更加某一字段排序 ,例如:@OrderBy(“date ASC”)
@OrderBy(“date ASC”)
private List orders;
@Property
设置一个非默认关系映射所对应的列名,默认是使用字段名,例如:
@Entity (nameInDb = “User”)
public class User {
@Property(nameInDb = “userName”)
private String userName;
}
@NotNull
设置数据库表当前列不能为空
@Transient
添加此标记后不会生成数据库表的列
@Unique
表名该属性在数据库中只能有唯一值
@Entity
public class User {
@Id
private Long id;
@Unique
private String name;
}
注意事项
由于greenDAO 3.0 生成的字段添加了非空约束。字段的类型设置为基本类型(如:int)默认会添加非空约束,字段类型设置为对象类型(如:Integer)默认不会添加非空约束,而且最终生成的sql会使用对象类型
定义int类型
@Entity
public class CommentList {
@Id(autoincrement = true)
private Long id;
private int sendId =0;//发送方ID
private int receiveId;//接收方ID
//------ CommentListDao.java ------
/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": “”;
db.execSQL("CREATE TABLE " + constraint + ““COMMENT_LIST” (” &#