Creating child table entry shared by multiple Parents?

Hi, in Java I am currently trying to create an instance for a table that is a child of multiple parents from a different table. I’m not sure exactly on the best approach although I am currently creating and saving the child entry first from the Backendless.Data.save(…) method, then I use that instance object saved and add into each of the parent objects relational properties (first getting their list of items), then I save the parent object(s) back with the saved child object added in the list that is the relational property (being 1:m). My issue though is I am getting a Exception: BackendlessException{ code: ‘8001’, message: ‘Duplicate property: actorsInMovie’. Im fairly sure I am following correctly tutorials from the documentation but I’m unable to see my error. Here is my code:

Actor savedActor= Backendless.Data.save(newActor); // Actor Instance saved returning this new Actor Instance.
StringBuilder whereClause = new StringBuilder();
whereClause.append(“Movie[actors]”);
whereClause.append(".objectId=’").append(movie.getObjectId()).append("’");
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause.toString());
actorsInMovie = Backendless.Data.of(Actor.class).find(dataQuery);
actorsList= actorsInMovie.getData();
actorsList.add(savedActor); // add new Actor to the movies actor list
movie.setActorsInMovie(actorsList); // … and this, add new List of actors for movie with new added actor added
Backendless.Data.save(movie); // Save and update movie with the new actor added

When I run this code it throws an exception on Backendless.Data.save(movie);, when I check the console backend the new actor is created but does not reference the movie as its parent instead throwing BackendlessException{ code: ‘8001’, message: ‘Duplicate property: actorsInMovie’. I should note that I’m trying to get several movies to be able to point to this same created actor without duplicate actors as soon as the actor is saved.

Hi, Leonard,

We’ll try to reproduce your issue and inform you about the results.

Thanks for that. Its just a case of me wanting to create child table entries and adding it/them as a related object for multiple parents (that could potentially already have related objects).

Could you please also provide your implementation of Movie and Actor classes?
I believe your problem is related to the following inconsistency:

    you search actors using actors field of Movie class:
whereClause.append( "Movie[actors]" );
    but you the seem to set property actorsInMovie of Movie class:
movie.setActorsInMovie( actorsList );

The code I gave you was an example of the problem but the concept still applies, I just replaced names. Here’s the actual code with the classes that exist from my app that is the real problem, Movie class = Person class and Actor Class = Slot Class, code implementation:

Slot savedSlot = Backendless.Data.save( slot );
StringBuilder whereClause = new StringBuilder();
whereClause.append(“Person[mycreatedslot]”);
whereClause.append(".objectId=’").append(personLoggedIn.getObjectId()).append("’");

BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause.toString());
loggedInUsersFSlots = Backendless.Data.of(Slot.class).find(dataQuery); // Gets the slots for the user
slotUserLoggedinAsList = loggedInUsersFSlots.getData();
slotUserLoggedinAsList.add(savedSlot); // Add new slot to existing slots
personLoggedIn.setMyCreatedSlot(slotUserLoggedinAsList); // Set slot list with new slot add
Backendless.Data.save(personLoggedIn); // Save user with new slots list

Person class:

public class Person {

public Contact mecontact;
public List<Contact> contacts;
public String objectId;
public List<Person> personscontacts;
public List<Slot> pendingresponseslot; // This property will apply to other users
public List<Slot> mycreatedslot; // This property being added to
public void addSlotToPendingResponseSlot(Slot slotItem) {
if (pendingresponseslot == null)
pendingresponseslot = new ArrayList<Slot>();
pendingresponseslot.add(slotItem);
}
public void addSlotToMyCreatedSlot(Slot slotItem) {
if (mycreatedslot == null)
mycreatedslot = new ArrayList<Slot>();
mycreatedslot.add(slotItem);
}
public List<Slot> getMyCreatedSlot() {
return mycreatedslot;
}
//This method being implemented for adding setting new list for user
public void setMyCreatedSlot(List<Slot> mycreatedslot) {
this.mycreatedslot = mycreatedslot;
}
public List<Slot> getPendingResponseSlot() {
return pendingresponseslot;
}
public void setPendingResponseSlot(List<Slot> pendingresponseslot) {
this.pendingresponseslot = pendingresponseslot;
}
public List<Person> getPersonsContacts() {
return personscontacts;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public Contact getMecontact() {
return mecontact;
}
public void setMeAsContact(Contact mecontact) {
this.mecontact = mecontact;
}
public String getObjectId() {
return objectId;
}
}
Slot class:

public class Slot {
public String ownerId;
public String objectId;
public String owneremailaddress;

public String getObjectId() {
return objectId;
}
public String getOwnerId() {
return ownerId;
}
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
}

I also have a method that just adds one slot item into the list and if the list is empty creates a new list, this might be more efficient for the purpose.The exception for the above code was BackendlessException{ code: ‘8001’, message: ‘Duplicate property: myCreatedSlot’ }.

I believe your problem is related to the following inconsistency:

    you search actors using actors field of Movie class:
whereClause.append( "Movie[actors]" );

Your correct, wouldn’t it be necessary to get the actors/mycreatedslot for the Movie/Userloggedin, since I want to add to that that list?

And for:

but you the seem to set property actorsInMovie of Movie class:

movie.setActorsInMovie( actorsList );

This is correct as well, since I am setting the list to the original object and then saving.

I think I’ve managed to solve this issue, there was another property with capitalized as the same property name which I removed.