How to update a relationshipt in an existing objects

Hi,
I’m trying to update a relationship from an existing table. I have a table Beach, User and Comment. That I’m trying to do is add a new comment and update at the same time in the existing Beach and User table. I know that maybe is easy but I’ve spent a lot of hours with this and with the documentation I can’t find a good way to do this (sorry but I’m some new in android). And I’m not really sure how can I add the current user when try to update the data.

The relationships that I have are: User --1-----N-- Comment --N------1-- Beach

And this is that I’m trying to do: (beachId is a string that have the objectId that I get in the request that I do when start the app) The app is crashing in the first line when I’m trying to do the findById() . I’m doing all this stuff in oncreate method.

final Beach currentBeach = Backendless.Persistence.of( Beach.class ).findById(beachId);
updateComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Beach updateCommentBeach = Backendless.Persistence.save(currentBeach);
BackendlessUser user = Backendless.UserService.CurrentUser();
Comment comment = new Comment();
comment.setMessage(“trying to update a comment”);
comment.setAuthorEmail(preferences.getString(MainActivity.USER_NAME, “”));
updateCommentBeach.getComment().add(comment);
Backendless.Persistence.save(updateCommentBeach);

}
});

Code of Beach class (setter and getter not paste here):
private String city;
private String country;
private String name;
private String description;
private GeoPoint location;
private String distance;
private String duration;
private ArrayList<Comment> comment;
private String objectId;

Code of Comment class (setter and getter not paste here):
private String message;
private String authorEmail;
private String objectId;

Code of User Class;
private String objectId;
public String getEmail() {
return super.getEmail();
}
public void setEmail( String email ) {
super.setEmail( email );
}
public String getPassword() {
return super.getPassword();
}
public String getName() {
return (String) super.getProperty( “name” );
}
public void setName( String name ) {
super.setProperty( “name”, name );
}

I’ve attached a screenshot of my comment table if you want to take a look.

Hi Salva,

When you try find beach by id, related entyties wouldn’t be retrieved. So before

Backendless.Persistence.save(updateCommentBeach);

you should make something like:


BackendlessDataQuery query = new BackendlessDataQuery(); 
QueryOptions queryOptions = new QueryOptions(); 
queryOptions.setRelationsDepth( 1 ); 
query.setQueryOptions( queryOptions ); 
query.setWhereClause( "objectId = " + &lt;objectId&gt; ); 
final Beach beachToUpdate = Backendless.Persistence.of( Beach.class ).find( query ).getData().get( 0 ); 
beachToUpdate.getComment().add(comment); 
Backendless.Persistence.save( beachToUpdate );

Regards,

Denys

Hi Denys,

Thanks for your reply.

According to your suggestion I’m doing this:

    updateComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            
            BackendlessDataQuery query = new BackendlessDataQuery();
            QueryOptions queryOptions = new QueryOptions();
            queryOptions.setRelationsDepth(1);
            query.setQueryOptions( queryOptions );
            query.setWhereClause( "objectId = " + beachId );


            Comment comment = new Comment();
            comment.setMessage("trying to update a comment");
            comment.setAuthorEmail(preferences.getString(MainActivity.USER_NAME, ""));


            final Beach beachToUpdate = Backendless.Persistence.of( Beach.class ).find(query).getData().get(0);
            beachToUpdate.getComment().add(comment);
            Backendless.Persistence.save(beachToUpdate);
        }
    });

but I’m getting this error in (final Beach beachToUpdate = Backendless.Persistence.of( Beach.class ).find(query).getData().get(0):wink:

BackendlessException{ code: ‘Internal client exception’, message: ‘null’ }
at com.backendless.Invoker$SyncResponder.errorHandler(Invoker.java:127)
at com.backendless.core.responder.AdaptingResponder.errorHandler(AdaptingResponder.java:93)
at weborb.client.ioEngine.HttpIOEngine.send(HttpIOEngine.java:213)
at weborb.client.ioEngine.HttpIOEngine.invoke(HttpIOEngine.java:145)
at weborb.client.WeborbClient.invoke(WeborbClient.java:138)
at com.backendless.Invoker.invokeSync(Invoker.java:100)
at com.backendless.Persistence.find(Persistence.java:603)
at com.backendless.DataStoreFactory$1.find(DataStoreFactory.java:161)
at com.exagon.touristbeach.CommentFragment$1.onClick(CommentFragment.java:100)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

One more thing that I’ve question in my first question. I want to set the comment with his current user in the same request. I have to create a method to setUser() in Comment table to can do it? and do BackendlessUser user = Backendless.UserService.CurrentUser(); to get the user???

comment.setUser(user);

Thanks!

I’ve got it. The error was because the method is not async. Change to async method work fine

I’m Sorry, I leave a new comment and not reply your answer. My answer is below