微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android GreenDAO会生成额外的ID属性

这是我的架构生成代码

    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”属性是重复的以及如何避免它?提前致谢

解决方法:

我犯了同样的错误.
目前您正在执行以下操作:

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

希望这可以帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐