2013-11-20 35 views
6

在greendao常見問題中,它說:「從greenDAO開始,對字符串主鍵的支持有限。」 http://greendao-orm.com/documentation/technical-faq/greendao字符串主鍵 - 如何使用

我找不到任何地方,說如何做到這一點。

我使用的GUID作爲我在服務器應用程序的主鍵,並希望能夠從Android設備遠程生成新的數據和上傳此回服務器。 android設備上的數據庫位於sqlite中,並使用greenDAO生成POJO和數據訪問層。數據上傳到服務器時,我使用Guids來避免主鍵衝突。我將Guids存儲爲字符串。

在greendao網站上還有一些建議,說我應該創建一個包含字符串的輔助字段,並仍然使用greendao支持的長主鍵,但這意味着我在導入時必須重新連接所有數據庫關係數據從服務器到應用程序,這是一個痛苦。如果可能的話,寧可繼續使用字符串主鍵。

有誰能告訴我該怎麼做?

下面是一些示例代碼...

在我的發電機(我已經去除了大部分爲清楚起見,場):

private static void addTables(Schema schema) 
{ 
    Entity unit = addUnit(schema);  
    Entity forSale = addForSale(schema); 

    Property unitIntId = forSale.addLongProperty("unitIntId").getProperty(); 
    forSale.addToOne(unit, unitIntId); 
} 


private static Entity addForSale(Schema schema) 
{ 
    Entity thisEntity = schema.addEntity("ForSale"); 
    thisEntity.addIdProperty(); 
    thisEntity.addStringProperty("forSaleId");   
    thisEntity.addFloatProperty("currentPriceSqFt");   
    thisEntity.addStringProperty("unitId"); 
    return thisEntity; 
} 

private static Entity addUnit(Schema schema) 
{ 
    Entity thisEntity = schema.addEntity("Unit"); 
    thisEntity.addIdProperty(); 
    thisEntity.addStringProperty("unitId"); 
    thisEntity.addStringProperty("name"); 
    return thisEntity; 
} 

在我的Android應用程序,我從下載的所有數據服務器。它具有基於GUID ID的關係。我不得不重新安裝這些在發電機產生這樣的INT編號的我:

  //Add relations based on GUID relations 

      //ForSale:Units 
      for(ForSale forSale:Globals.getInstance().forSales) 
      { 
       if (forSale.getUnitId() != null && forSale.getUnit() == null) 
       { 
        for(Unit unit:Globals.getInstance().units) 
        { 
         if (forSale.getUnitId().equals(unit.getUnitId())) 
         { 
          forSale.setUnit(unit); 
          break; //only need the first one 
         } 
        } 
       } 
      } 

所以我最終兩套標識的鏈接應有盡有,INT一個greendao和字符串(GUID)一個會的當它被上傳回服務器時工作。一定是一個更簡單的方法!

+0

你已經試過了嗎? – AlexS

+0

@AlexS - 我已經在上面添加了一些示例代碼。有任何想法嗎? – BobbyG

+0

該映射是否也可以工作? – AlexS

回答

11

試試這個:

private static void addTables(Schema schema) { 
    Entity unit = addUnit(schema);  
    Entity forSale = addForSale(schema); 

    Property unitId = forSale.addStringProperty("unitId").getProperty(); 
    forSale.addToOne(unit, unitId); 
} 

private static Entity addForSale(Schema schema) { 
    Entity thisEntity = schema.addEntity("ForSale"); 
    thisEntity.addStringProperty("forSaleId").primaryKey();   
    thisEntity.addFloatProperty("currentPriceSqFt");   
    return thisEntity; 
} 

private static Entity addUnit(Schema schema) { 
    Entity thisEntity = schema.addEntity("Unit"); 
    thisEntity.addStringProperty("unitId").primaryKey(); 
    thisEntity.addStringProperty("name"); 
    return thisEntity; 
} 

我不知道,如果對於某測繪圖將與字符串的工作,雖然。如果沒有,您可以添加一些方法來獲取KEEP-SECTIONS中的相關對象。

+1

奇妙! .primaryKey()是答案。 toOne映射工作正常。我也嘗試了一對一的映射,它也能正常工作(只需在適當的位置修改long - > string)。這節省了我數小時或編寫樣板代碼。你碰巧知道哪裏有更全面的手冊,因爲我找不到任何關於此的內容,實際上該網站暗示你只能使用多頭。我目前只有greendao網站上的例子可以繼續。 – BobbyG

+0

太好了。我以前沒有嘗試過,而且時間太短,無法仔細觀察。不幸的是,我不知道任何其他的greendao手冊。一些有用的提示和示例可以在google-group greendao中找到。當然,我也在使用javadoc。幸運的是,greendao是開源的。如果你對某些行爲感到疑惑,你可以在消息來源中獲得樂趣。由於greendao不使用反射,代碼非常簡單。 – AlexS

+0

我完全和BobbyG一樣(我使用UUID來避免Android應用程序和我的網站之間的主鍵衝突)。非常感謝您的幫助!主鍵作爲字符串,一切正常。 – Guicara