The greendo FAQ says, "Starting with greenDAO, there is limited support for String primary keys." http://greendao-orm.com/documentation/technical-faq/
I cannot find anywhere where it says how to do this.
I use Guides as the main key in the server application and I want to be able to create new data remotely from an Android device and upload it back to the server. The database on the Android device is located in sqlite and uses greenDAO to create the POJO and data access level. I use Guides to avoid collisions with primary keys when data is uploaded to the server. I keep Guides as strings.
There are some more tips on the greendao website that say I should create a secondary field containing the string and use the long primary key approved by greendao, but that means I have to reconnect all my database relationships when importing data from the server to the application, which is a pain. Most likely, just keep using the primary keys of the string, if possible.
Can anyone tell me how to do this?
Here is a sample code ...
In my generator (I just deleted most of the fields):
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; }
In my Android application, I download all the data from the server. It has a relationship based on GUIDs. I have to re-bind them to the int Id that 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 } } } }
So, I have two sets of Id connecting everything, int one for greendao and a string (guid) that will work when it is loaded back to the server. There should be an easier way!