1

I am currently switching from ORMlite to GreenDao and I have trouble creating my database model in greendao.

My content is fetched from a server where the server identifies the entries with generated primary keys as String. My internal database has primary keys as Long (like Android requires).

When I sync, I need to create relations based on the keys (here "backendId") delivered by the server to make my sync way way easier.

In ORMlite I could say which column should be used to create the relationship on. The same I try to create for greendao but I fail.

Here is what I try: A sector can have an asset. The "assetId" should contain the value that is stored in "backendId" of Asset.

Any ideas how I can achieve that?

Entity asset = schema.addEntity("Asset");
asset.setSuperclass("Backend");
asset.addIdProperty().index();
asset.addStringProperty("backendId").index();
asset.addStringProperty("title");
asset.addStringProperty("description");
asset.addStringProperty("contentType");
asset.addStringProperty("url");

Entity sector = schema.addEntity("Sector");
sector.setSuperclass("Backend");
sector.addIdProperty().index();
sector.addStringProperty("backendId").index();
sector.addStringProperty("title");
sector.addToOne(asset, sector.addLongProperty("asset").getProperty(), "assetId");

// insert test:
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "greenmodel-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
AssetDao assetDao = daoSession.getAssetDao();
SectorDao sectorDao = daoSession.getSectorDao();

Asset asset = new Asset(1l, "12345", "Title", "Description", "type", "url");
assetDao.insert(asset);

Sector sector = new Sector();
sector.setTitle("title");
sector.setBackendId("123");
sector.setAssetId(asset);
sectorDao.insert(sector);

The result of that insert is, that the "assetId" is the primary key of the asset table but I need it to be the "backendId".

Any ideas?

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150

1 Answers1

4

At the moment your only chance may be to use String-primary keys (if that's possible with your data).

You can try something like this:

Entity asset = schema.addEntity("Asset");
asset.setSuperclass("Backend");
asset.addStringProperty("backendId").index().primaryKey();
asset.addStringProperty("title");
asset.addStringProperty("description");
asset.addStringProperty("contentType");
asset.addStringProperty("url");

Entity sector = schema.addEntity("Sector");
sector.setSuperclass("Backend");
sector.addIdProperty().index();
Property fkAsset = sector.addStringProperty("backendId").index();
sector.addStringProperty("title");
sector.addToOne(asset, fkAsset, "Asset");

And then use this to test:

Asset asset = new Asset("12345", "Title", "Description", "type", "url");
assetDao.insert(asset);

Sector sector = new Sector();
sector.setTitle("title");
sector.setAsset(asset);
sectorDao.insert(sector);

Update

I know greendao does not garantee to work correctly with using other primary keys than Long. But building relations seem to work, although I never tested it myself. See this post. I had a look at the sources and I didn't find a reason, why it should not work.

Looking at the sources I don't think referencing something other than the primary key is possible at the moment.

If you want to use the standard id primary key a workaround would also be to calculate the ids from the string values in the keep sections and use a "normal" greendso toOne mapping.

Community
  • 1
  • 1
AlexS
  • 5,295
  • 3
  • 38
  • 54
  • I thought about that way but was quite unsure about that statement in their introduction: "Current primary key (PK) restrictions: Currently, entities must have a long or Long property as their primary key. This is recommended practice for Android and SQLite. greenDAO is prepared to handle any primary key scenario in the future, but not everything is implemented completely yet. To work around this issue, you can use a long primary key and use an unique index for the intended “key” properties." (Source: http://greendao-orm.com/documentation/modelling-entities/) Any clue about "key" properties? – WarrenFaith May 20 '14 at 15:03
  • @WarrenFaith I know that greendao currently states, that a long has to be used as primary key, but it seems to work nevertheless. See my update. – AlexS May 21 '14 at 11:21
  • Thank you for the update. The "generate key" workaround sounds like a nice idea but if there is a chance that I can totally skip the internal/external id stuff, that would be great. I will give switching to strings as keys a try and see if I hit the wall somewhere. – WarrenFaith May 21 '14 at 11:25
  • @WarrenFaith You are welcome. Please let me know if it works as expected. I am curious about it myself, but I don't have the time to check it. – AlexS May 21 '14 at 12:53
  • I was exactly in the same situation that WarrenFaith (I use UUID to avoid primary key collisions between my Android app and my website). Thank you very much for your help! Everything is OK with primary key as string. – Guicara Jul 13 '14 at 12:22