greendao字符串主键 - 如何使用字符串、如何使用、主键、greendao

2023-09-07 04:30:30 作者:迷糊小生

在greendao常见问题解答它说:从greenDAO开始有弦乐主键的支持有限。 http://greendao-orm.com/documentation/technical-faq/

In the greendao FAQs it says "Starting from greenDAO there’s limited support for String primary keys." http://greendao-orm.com/documentation/technical-faq/

我找不到任何地方,上面写着如何做到这一点。

I can't find anywhere that says how to do this.

我用的GUID在服务器应用程序我的主键,并希望能够从Android设备远程生成新的数据,并上传此回服务器。在Android设备上的数据库SQLite中,并使用greenDAO生成POJO和数据访问层。我使用的GUID,以避免主键冲突时,数据被上传到服务器。我存储的GUID为字符串。

I am using Guids as my primary key in a server application, and want to be able to generate new data remotely from an android device and upload this back to the server. The database on the android device is in sqlite and uses greenDAO to generate POJOs and data access layer. I am using Guids to avoid primary key collisions when data is uploaded to the server. I am storing the Guids as strings.

对于所用greendao网站说我应该创建一个辅助场拿着绳子,仍然使用由greendao青睐长期主键一些建议,但是这也意味着我必须重新连接我所有的数据库关系,当我导入数据从服务器到该应用这是一种痛苦。宁愿只是继续使用字符串主键,如果这是可能的。

There is some more advice on the greendao website that says I should create a secondary field holding the string and still use the long primary key favoured by greendao, but this means that I have to reconnect all my database relationships when I import data from the server to the app which is a pain. Would much rather just continue to use the string primary keys if that is possible.

谁能告诉我该怎么做呢?

Can anybody tell me how to do this?

下面是一些例子code ...

Here is some example code...

在我的发电机(我已删除大部分为清楚起见字段):

In my generator (I've removed most of the fields for clarity):

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

在我的Andr​​oid应用程序,我下载从服务器中的所有数据。它具有基于GUID ID的关系。我必须重新连接这些在发生这样创建的INT编号的我:

In my android application I download all the data from the server. It has relationships based on the GUID id's. I have to reattach these to the int Id's I created in the generator like this:

            //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)之一,将工作时,它被上传到服务器中。必须是一个更简单的方法!

So I end up having two sets of Id's linking everything, the int one for greendao and the string (guid) one that will work when it gets uploaded back to the server. Must be an easier way!

推荐答案

试试这个:

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

我不知道,如果对于某-映射将与字符串工作,虽然。如果没有,你可以添加一些方法来获取相关对象在保的部分。

I don't know if the ToOne-Mapping will work with strings, though. If it doesn't you can add some methods for getting the related objects in the KEEP-SECTIONS.