Alex Waugh 2 months ago • updated by Michał Kołodziejski (CTO) 2 months ago 1
I am using Vertabelo Mobile ORM with Android.
I have two tables that share a one to many relationship. i.e. Table2 has an FK that references the PK from Table1, which is an auto-incrementing id value.
The normal record creation process would be:
  1. Create instance of Table1
  2. DAO.insert(Table1)
  3. Create instance of Table2
  4. Create the relationship by setting the FK value in Table2 to Table1.getid()
  5. DAO.insert(Table2)

However, I only want a local representation of the records in java, which I may or may not decide to save to the database later, depending on user input. However, I can't do this because I can't create the relationship between them, as the auto-incremented id is only created when the record is saved to the db (step 2).
That means that I can't use Table1.getTable2List(), because the relationship between them does not exist.

Is it even possible to use methods that rely on a relationship between tables, such as get...List(), on local java objects, before they are saved to the db?
If so, is there a way to do this when the FK is an auto-incrementing?

Otherwise, I like the ORM so far! I am new to both Android and Vertabelo and it seems to be saving me some time already so thanks :)

Satisfaction mark by Alex Waugh 2 months ago

The classes Table1 and Table2 generated by Vertabelo Mobile ORM are simple POJOs. Their role is to be DTOs reflecting database row data. Our ORM is more database-centric rather than object-centric. It means that, just like in the DB, the relation between objects is based on PK/FK data, not on Java object references.

Given the above, when you rely on autogenerated PKs, there's no other way to identify these not-inserted-yet rows. So, in the case you described, there would be no way to achieve what you want.

Hope this helps.