Set related object without finding it

Hi, for setting related object I can use something like that:

Contact owner = Backendless.Data.of(Contact.class).findById(objectId);
...
PhoneBook phoneBook = new PhoneBook();
phoneBook.setOwner(owner);
PhoneBook savedPhoneBook = Backendless.Data.of(PhoneBook.class).save(phoneBook);

Can I set related object without finding Contact object it’s a bit time-consuming, especially when I need find a lot of objects?

Thanks!

Hi Oleh,

Assuming you know the Contact’s object ID, you may do the following:

Contact owner = new Contact();
owner.setObjectId( objectId );
...
PhoneBook phoneBook = new PhoneBook();
phoneBook.setOwner(owner);
PhoneBook savedPhoneBook = Backendless.Data.of(PhoneBook.class).save(phoneBook);\

The point is that you only need to have a correct objectId value in the object to set up the relation.

Hi Sergey, I tried this approach but after I saved PhoneBook, fields in Contact table with this objectId become null

Oh, you’re right, the objects will be all updated on this call. Then I suppose there’s no other way than to retrieve the whole object.

Ok, then I’ll retrieve whole object, but thanks anyway