GreenDAO字符串主键-如何使用

6
在greendao的常见问题解答中,它说“从greenDAO开始,对于字符串主键的支持有限”。http://greendao-orm.com/documentation/technical-faq/ 我找不到任何地方说明如何做到这一点。
我正在一个服务器应用程序中使用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 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
                        }
                    }
                }
            }

所以最终我有两套ID将所有内容链接起来,一个是用于GreenDao的int类型,另一个是字符串(guid),在上传到服务器时可正常使用。一定有更简单的方法!

你已经尝试过什么了吗? - AlexS
@AlexS - 我在上面添加了一些示例代码。有什么想法吗? - BobbyG
映射工作正常吗? - AlexS
是的,映射也可以像我在下面所选答案的评论中提到的那样工作。再次感谢。 - BobbyG
1个回答

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

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


1
太棒了!.primaryKey()就是答案。toOne映射正常工作。我还尝试了一对多的映射,也正确地工作了(只需要在适当的位置将long->string更改)。这为我节省了数小时的样板代码编写时间。你知道有没有更全面的手册吗?我找不到任何相关信息,事实上网站暗示你只能使用longs。目前我只有greendao网站上的例子可以参考。 - BobbyG
很好。我以前没有尝试过并且时间太短,无法仔细研究它。不幸的是我不知道greendao的其他手册。一些有用的提示和示例可以在google-group greendao中找到。当然我也在使用javadoc。幸运的是,greendao是开放源代码的。如果您对某些行为感到困惑,可以查看源代码。由于greendao不使用反射,所以代码非常直观。 - AlexS
我和BobbyG处于完全相同的情况(我使用UUID来避免Android应用程序和网站之间的主键冲突)。非常感谢您的帮助!主键作为字符串时一切正常。 - Guicara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接