在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)一個會的當它被上傳回服務器時工作。一定是一個更簡單的方法!
你已經試過了嗎? – AlexS
@AlexS - 我已經在上面添加了一些示例代碼。有任何想法嗎? – BobbyG
該映射是否也可以工作? – AlexS