Save new instance with existing relation

Hi,
I would like to do the following:
I have a class exercises (simplified for this question):

public class Exercise {



 private String objectId;
 private Agegroup agegroup;



 public String getObjectId() {
 return objectId;
 }



 public void setObjectId(String objectId) {
 this.objectId = objectId;
 }



 public Agegroup getAgegroup() {
 return agegroup;
 }



 public void setAgegroup(Agegroup agegroup) {
 this.agegroup = agegroup;
 }
}

And a class Agegroup:

public class Agegroup {



 private String objectId;
 private String agegroup;



 public String getObjectId() {
 return objectId;
 }



 public void setObjectId(String objectId) {
 this.objectId = objectId;
 }



 public String getAgegroup() {
 return agegroup;
 }



 public void setAgeGroup(String ageGroup) {
 this.agegroup = agegroup;
 }



 public Agegroup save() {
 return Backendless.Data.of(Agegroup.class).save(this);
 }
}

Now the user should be able to add a new exercise with a relation to an existing agegroup Possibilities are: <14y, 14 - 18y, >18y, all, which have already been added to the database manually.
I tried using

Exercise exercise = new Exercise();
exercise.setAgegroup(objectId of the selected agegroup)
Backendless.Persistence.save(exercise, new AsyncCallback&lt;Exercise&gt;() {
@Override

public void handleResponse(Favorites favorites) {
      Log.d("SAVED", "SAVED");
}


@Override
public void handleFault(BackendlessFault backendlessFault) {
      Log.d("SAVED", backendlessFault.getMessage());
}
});

But it doesn’t work…
Anybody can help me?
Greetings

Hi Birger,

Try following:


Agegroup agegroup = Backendless.Persistence.of( Agegroup.class ).findById( &lt;objectId of the selected agegroup&gt;  );
Exercise exercise = new Exercise();
exercise.setAgegroup(agegroup);
Backendless.Persistence.save(exercise, new AsyncCallback&lt;Exercise&gt;() {...});

Regards,

Denys

Using that gave me an error:

BackendlessException{ code: 'Internal client exception', message: 'null' }

I used an asynchronous call (this is from my application so it’s different from my question):

public void addToFavorites() {
   showSnackbar(getResources().getString(R.string.info_adding_exercise_to_favorites));

    final Favorites favorites = new Favorites();

   BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery();

   String whereClause = "objectId='" + exerciseObjectId + "'";
   backendlessDataQuery.setWhereClause(whereClause);

   Backendless.Persistence.of(Exercise.class).find(backendlessDataQuery, new AsyncCallback&lt;BackendlessCollection&lt;Exercise&gt;>() {
   @Override
    public void handleResponse(BackendlessCollection&lt;Exercise&gt; response) {
       for (Exercise exercise : response.getCurrentPage()) {
         favorites.setExercise(exercise);
      }

      Backendless.Persistence.save(favorites, new AsyncCallback&lt;Favorites&gt;() {
      @Override
       public void handleResponse(Favorites response) {
         showSnackbar(getResources().getString(R.string.info_exercise_saved_successfully));
          floatingActionButton.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_fab_star, null));
          favorited = true;
          favoritesObjectId = favorites.getObjectId();
 }

         @Override
          public void handleFault(BackendlessFault fault) {
            showSnackbar(fault.getMessage());
            Log.d("FAULT: ", fault.getMessage());
         }
      });

   }

   @Override
     public void handleFault(BackendlessFault fault) {
      showSnackbar(fault.getMessage());
      Log.d("FAULT: ", fault.getMessage());
   }
 });
}

Sometimes however it appears to be really slow… and sometimes just normal fast…

Hello Birger,

Using that gave me an error:
which line exactly provides an error?

Sometimes however it appears to be really slow… and sometimes just normal fast…
it is hard to say about the reason of such behavior. you can move to managed backendless version to isolate your envirment. please contact sales@backendless.com to get more details about opportunities which backendless provides with managed version

Agegroup agegroup = Backendless.Persistence.of( Agegroup.class ).findById( &lt;objectId of the selected agegroup&gt; );

Gives an error: BackendlessException{ code: ‘Internal client exception’, message: ‘null’ }

I guess because it’s a synchronous call, while a synchronous call should run on a new Thread?

And just 1 quick question: is it better to use relationships or just put all data in the same table?

Same example as last time: I have exercises which have an agegroup (and like 8 more specifications all coming from a different relationship [classification, distancegroup,…]). I always learned it the relational way, but those were MySQL databases, this service is new to me and I don’t know which practices are right or wrong.

Birger,

You are correct, the “internal error” occurs because you’re using sync call. Please change it to the asynchronous signature.

As for the data normalization, think about it from the app’s (user experience) perspective. Do your users need to see all the data at once? Or perhaps you need to get only objects of certain kind first and then load some related data. I believe it is the UI and the user experience (UX) that should drive the data flow and dictate how the data is structured.

Hope this helps.

Regards,
Mark