I’ve got a one to one relationship between 2 Tables (A,B).
I usually create a new instance of A and add a new instance of B.
Then I save A, all fine.
Now consider I’d like to save a new instance of A but add an existing instance of B this time.
I retrieve object B from the Backend, add it to object A and the save object A.
The problem is that this way, a new instance of object B is being created. (redundancies)
Just to test this i switched the id of object B to unique.
In this case I get: BackendlessException{ code: ‘36’, message: ‘Duplicate entry’ }
How to overcome this issue? Am I misunderstanding something?
Is there a way to make a “create OR update” operation?
Sorry, I could reproduce the problem. Here’s my code:
package com.mbaas;
import com.backendless.Backendless;
import java.util.ArrayList;
import java.util.List;
public class SaveRelation
{
public static void main( String[] args ) throws Exception
{
Backendless.initApp( APP_ID, SECRET_KEY, "v1" );
// STEP 1 - run this method first
// initialSetup();
// STEP 2 - if you run the program more than once, make sure to comment out STEP1
saveRelationWithExistingChild();
}
private static void saveRelationWithExistingChild()
{
Person person = new Person();
person.setName( "Bob" );
Address address = Address.findFirst();
List<Address> addresses = new ArrayList<Address>();
addresses.add( address );
person.setAddresses( addresses );
person.save();
}
private static void initialSetup()
{
Person person = new Person();
person.setName( "Bob" );
Address address = new Address();
address.setCity( "Dallas" );
List<Address> addresses = new ArrayList<Address>();
addresses.add( address );
person.setAddresses( addresses );
person.save();
}
}
I also used code created by code generator. The schema for my tables looks like this: