Android GreenDAO生成额外的ID属性

Android GreenDAO生成额外的ID属性,android,database,sqlite,greendao,Android,Database,Sqlite,Greendao,这是我的架构生成代码: Schema schema = new Schema(VERSION, "com.example.dao"); Entity player = schema.addEntity("Player"); Property playerIdProperty = player.addStringProperty("id").primaryKey().getProperty(); player.addStringProperty("name").n

这是我的架构生成代码:

    Schema schema = new Schema(VERSION, "com.example.dao");

    Entity player = schema.addEntity("Player");
    Property playerIdProperty = player.addStringProperty("id").primaryKey().getProperty();
    player.addStringProperty("name").notNull();
    player.addStringProperty("created_at");
    player.addStringProperty("updated_at");

    Entity match = schema.addEntity("Match");
    match.addStringProperty("id").primaryKey();
    match.addIntProperty("score1");
    match.addIntProperty("score2");
    match.addStringProperty("created_at");
    match.addStringProperty("updated_at");

    match.addToOne(player, playerIdProperty, "dp1");
    match.addToOne(player, playerIdProperty, "dp2");
    match.addToOne(player, playerIdProperty, "op1");
    match.addToOne(player, playerIdProperty, "op2");


    new DaoGenerator().generateAll(schema, "app/src-gen");
这就是它产生的结果:

public class MatchDao extends AbstractDao<Match, String> {

public static final String TABLENAME = "MATCH";

/**
 * Properties of entity Match.<br/>
 * Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
    public final static Property Id = new Property(0, String.class, "id", true, "ID");
    public final static Property Score1 = new Property(1, Integer.class, "score1", false, "SCORE1");
    public final static Property Score2 = new Property(2, Integer.class, "score2", false, "SCORE2");
    public final static Property Created_at = new Property(3, String.class, "created_at", false, "CREATED_AT");
    public final static Property Updated_at = new Property(4, String.class, "updated_at", false, "UPDATED_AT");
    public final static Property Id = new Property(5, String.class, "id", true, "ID");
};
公共类MatchDao扩展了AbstractDao{
公共静态最终字符串TABLENAME=“MATCH”;
/**
*实体匹配的属性。
*可用于QueryBuilder和引用列名。 */ 公共静态类属性{ 公共最终静态属性Id=新属性(0,String.class,“Id”,true,“Id”); 公共最终静态属性Score1=新属性(1,Integer.class,“Score1”,false,“Score1”); 公共最终静态属性Score2=新属性(2,Integer.class,“Score2”,false,“Score2”); public final static Property Created_at=新属性(3,String.class,“Created_at”,false,“Created_at”); 公共最终静态属性Updated_at=新属性(4,String.class,“Updated_at”,false,“Updated_at”); 公共最终静态属性Id=新属性(5,String.class,“Id”,true,“Id”); };
如您所见,在MatchDao中有两个名为“Id”的属性。我需要做的是生成两个主键为字符串的表(字符串是远程数据库的要求)和4个外键(每个匹配有4个玩家)


问题是:为什么“Id”属性会重复以及如何避免它?提前感谢我不确定,但是这个Id属性不会重复。这个“Id”属性是与实体“Player”1:1关系的结果。也许
addToOne(实体目标,属性fkProperty,java.lang.String名称)中有一个bug
方法。

我犯了和你以前一样的错误。 目前,您正在执行以下操作:

Entity player = schema.addEntity("Player");
Property playerIdProperty = player.addStringProperty("id").primaryKey().getProperty();
...
match.addToOne(player, playerIdProperty, "dp1");
但您必须将playerId属性添加到目标类:

Entity player = schema.addEntity("Player");
player.addStringProperty("id").primaryKey();
...
Entity match = schema.addEntity("Match");
Property player1IdProperty = match.addLongProperty("dp1").getProperty();
...
match.addToOne(player, player1IdProperty);
希望这有帮助