Hi!
I have two tables, A and B with the following feature.
Table A has a column called B_OBJECTS, which relates an object of table A with one or more objects of table B.
So there is a relation one to many between A and B, described by the column B_OBJECTS in table A.
When i try to update the list of B objects in B_OBJECTS, i have a weired behaviour.
I update the A object retriving it by a findById query, then i modify the B_OBJECTS list containing the B objects related with A.
During debug the A object is correctly modified, but when i run the Backendless.Data.of(A.class).save(this), the returned A object has the B_OBJECTS attribute set to null.
It’s pretty weird because i run the same istructions on other tables with the same relations (one to many) and it works fine.
Here you are the code for loading the already existing A object and updating it:
A aToUpdate = Backendless.Data.of(A.class).findById(objectId, 1);
if (aToUpdate != null)
A updatedA = aToUpdate.update(jsonnedData);
And here is the update method implemented in the A class:
public A update(JSONObject json) {
try {
if (json.has("name"))
this.name = json.getString("name");
if (json.has("idB_OBJECTS") && json.getJSONArray("idB_OBJECTS").length() > 0) {
JSONArray jsonArray = json.getJSONArray("idB_OBJECTS");
StringBuilder whereClause = new StringBuilder();
whereClause.append("objectId='").append(jsonArray.getString(0)).append("'");
for (int i = 1; i < jsonArray.length(); i++)
whereClause.append(" OR ").append("objectId='").append(jsonArray.getString(i)).append("'");
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause.toString());
List<B> bList= Backendless.Persistence.of(B.class).find(dataQuery).getData();
if (bList!= null && !bList.isEmpty()) {
if (this.B_OBJECTS!= null)
this.B_OBJECTS.clear();
else
this.B_OBJECTS = new ArrayList<>();
this.B_OBJECTS.addAll(bList);
}
}
} catch (BackendlessException e) {
e.printStackTrace();
}
return Backendless.Data.of(A.class).save(this);;
}
Can you help me?
Thanks