Support Topics Documentation Slack YouTube Blog

Add object and it's one to one relation in one shot


(SnakeEyes) #1

Hello
To create new object A with one to one relationship to another object B
I do the following:
Add A
Get the map of A
Set relationship of A to B and save

Is there a way I can add A with its relationship in one shot so I don’t have to do 2 operations (ie save then set relationship). I am just concerned that I add A and something wrong happens so I am left with A without a child relation.

Thank you


(Vladimir Yalovy) #2

Hello @snakeeyes

You use code something like this:

Map savedParentObjectA = Backendless.Persistence.of( "TableA" ).save( new HashMap<>() );

Map savedChildObjectB = Backendless.Persistence.of( "TableB" ).save( new HashMap<>() );

Integer result = Backendless.Persistence.of( "TableA" ).addRelation(
            savedParentObjectA,
            "child", Collections.singletonList( savedChildObjectB ) );

?


(SnakeEyes) #3

Hi Vladimir
Right now yes. I use similar code (except that B already exists in the table ).

I am wondering if I can add A and set its relation in one call ?

Thank you


(Oleg Vyalyh) #4

You can also implement necessary logic using api services.
Here is an example:
https://backendless.com/how-to-save-an-object-with-all-the-children-in-a-single-backendless-call/


(Anton Govorushkin) #5

@snakeeyes if your goal is to create an object and to set relation in one Backendless API call then there’s no way it can be done. If it’s a convenience issue - then you may look into approach Oleg suggested above


(SnakeEyes) #6

Thank you. It is more than convinience. It is incase one operation failed which will result in data of one successful operation which makes the data corrupted. Maybe you guys can consider implementing “roll back option” like in SQL where you can commit everything once all operation succeeded.

Thanks again


(Oleg Vyalyh) #7

We have in our plans to add such function as a Transaction, where you’ll be able to accumulate necessary actions, that would be done atomically.


(SnakeEyes) #8

That would be great. Thank you so much!