2014-06-18 54 views
4

這是我的模式生成代碼:的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」屬性是重複的以及如何避免它?在此先感謝

回答

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)方法中有一個錯誤。

0

也許addToOne函數需要一個整數屬性? 我建議您按照下列步驟操作:

  • 更改您的主鍵。這將是外鍵太:

    Property playerIdProperty = player.addIdProperty().autoincrement().primaryKey().getProperty();` 
    
  • 創建唯一字符串標識:

    player.addStringProperty("identifier").notNull().unique(); 
    

有了這個,你仍然有兩個屬性,但我認爲該模型是更好的。我贊成使用數字編號。對不起我的英語不好。