greendao字符串主键 - 如何使用

Bob*_*byG 6 orm android greendao

在greendao常见问题解答中,它说"从greenDAO开始,对String主键的支持有限." http://greendao-orm.com/documentation/technical-faq/

我找不到任何说明如何做到这一点.

我使用Guids作为服务器应用程序中的主键,并希望能够从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;
}
Run Code Online (Sandbox Code Playgroud)

在我的Android应用程序中,我从服务器下载所有数据.它具有基于GUID id的关系.我必须将这些重新连接到我在生成器中创建的int Id,如下所示:

            //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
                        }
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

所以我最终有两组Id连接所有东西,一个用于greendao,另一个用于字符串(guid),当它被上传回服务器时将起作用.必须是一个更简单的方法!

Ale*_*exS 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;
}
Run Code Online (Sandbox Code Playgroud)

但我不知道ToOne-Mapping是否可以使用字符串.如果没有,您可以添加一些方法来获取KEEP-SECTIONS中的相关对象.