Android GreenDAO生成额外的ID属性

4
这是我的模式生成代码:

    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中有两个名为“Id”的属性。我需要做的是生成两个表,主键分别为字符串(因为远程数据库的要求)和4个外键(每场比赛有4个玩家)。

问题是:为什么“Id”属性会重复,并且如何避免它?谢谢提前。

2个回答

4

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

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);

希望这能帮到你!

0

我不确定,但这个id属性不是重复的。这个"id"属性是与实体“Player”的1:1关系的结果。也许在addToOne(Entity target, Property fkProperty, java.lang.String name)方法中存在一个错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接