Retrieving BackendlessUser from custom table class returns null

Hi all,

I am making a social app, so I made a custom table called Comments with a One-to-one relationship with Users table and a String column
http://support.backendless.com/public/attachments/14a9d4e5809d35554a61cb1dd105cf63.PNG</img>
and that table is a child of another table which called Posts which contains a one-to-many relationship with Comments table
but when I retrieve a Posts element which contains a List<Comments>, every commentedUser property in those comments in the list have the value of “null”
every table is mapped by code, autoload property in table is marked.
where does the problem come from? and is there a solution for it without adding just the objectId of the user instead of the relationship?

Hi Sherif,

What is the data type of the commentedUser property in the code?

Regards,
Mark

Hi Mark,

the data type of commentedUser property is BackendlessUser

Thanks, Sherif. You mentioned that “every table is mapped by code”, do you have a mapping for the Users table as well? If so, what does it look like?

Thanks for your reply, Mark
I read before that “Users” table should not be mapped because BackendlessUser is already working with it, but I will try to map it manually right now and see if it works

Hi Sherif,

I just ran a similar test and without any additional data mapping I was able to get related user objects. Do you mind posting your Comments class here?

Regards,
Mark

package com.reflektt.reflektt.Tables;

import com.backendless.BackendlessUser;

public class Comments{
    private BackendlessUser commentedUser;
    private String comment;

    public Comments(){

    }
    public BackendlessUser getCommentedUser() {
        return commentedUser;
    }

    public void setCommentedUser(BackendlessUser commentedUser) {
        this.commentedUser = commentedUser;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

}

And one more thing: could you check the version of the Backendless SDK library you are using?

At the moment I am working on SDK 3.0.24

I used the latest version of the SDK (3.0.25) and it worked for me.

Here’s what I tried:

I have a table called Venue, which has a one-to-many relation with table Restaurant, which has a one-to-one relation with Users:

Venue:
http://support.backendless.com/public/attachments/f6574092af2752b2b7183b3b92fd04eb.png&lt;/img&gt;

Restaurant:
http://support.backendless.com/public/attachments/9095e3e4a7f72ca8852fc08d5afabc66.png&lt;/img&gt;

Here’s my code which loads all venues and then logs the “owner” (which is a user) for each restaurant:

        Backendless.Data.of( Venue.class ).find(new AsyncCallback&lt;BackendlessCollection&lt;Venue&gt;>() {
            @Override
            public void handleResponse(BackendlessCollection&lt;Venue&gt; response) {
                for( Venue v : response.getCurrentPage() )
                {
                    List&lt;Restaurant&gt; restaurantList = v.getRestaurants();
                    for( Restaurant r : restaurantList )
                    {
                        Log.i( "MYAPP", "owner - " + r.getOwner() );
                    }
                }
            }
            @Override
            public void handleFault(BackendlessFault fault) {
Log.e( "MYAPP", fault.toString());
            }
        });

The log displays all the BackendlessUser objects as expected:

03-26 09:15:07.962 3916-3916/com.backendless.demoapp I/MYAPP: owner - BackendlessUser{playlists=[Ljava.lang.Object;@ea9b048, __meta={"relationRemovalIds":{},"selectedProperties":["password","eyeColor","created","name","playlists","dateOfBirth","ownerId","intColumn","updated","objectId","email"],"relatedObjects":{}}, dateOfBirth=null, eyeColor=brown, ___class=Users, email=bob@backendless.com, updated=null, name=null, ownerId=7F188846-61D2-44CE-FF5E-AEF835BE1000, intColumn=null, created=Wed Nov 02 15:59:25 CDT 2016, objectId=7F188846-61D2-44CE-FF5E-AEF835BE1000}
03-26 09:15:07.962 3916-3916/com.backendless.demoapp I/MYAPP: owner - BackendlessUser{playlists=[Ljava.util.HashMap;@d903c42, __meta={"relationRemovalIds":{},"selectedProperties":["password","eyeColor","created","name","playlists","dateOfBirth","ownerId","intColumn","updated","objectId","email"],"relatedObjects":{"playlists":["DC789E77-1ECC-0316-FF6D-0CB24461C800"]}}, dateOfBirth=null, eyeColor=brown, ___class=Users, email=mark@backendless.com, updated=null, name=null, ownerId=91456812-32B8-8EF6-FF3A-AE6249472D00, intColumn=null, created=Thu Mar 09 12:28:09 CST 2017, objectId=91456812-32B8-8EF6-FF3A-AE6249472D00}
03-26 09:15:07.962 3916-3916/com.backendless.demoapp I/MYAPP: owner - BackendlessUser{playlists=[Ljava.lang.Object;@e9a759f, __meta={"relationRemovalIds":{},"selectedProperties":["password","eyeColor","created","name","playlists","dateOfBirth","ownerId","intColumn","updated","objectId","email"],"relatedObjects":{}}, dateOfBirth=null, eyeColor=null, ___class=Users, email=joe@backendless.com, updated=null, name=null, ownerId=C8A79BDE-7113-4B10-FF4D-7960C68FD000, intColumn=null, created=Wed Nov 02 15:48:27 CDT 2016, objectId=C8A79BDE-7113-4B10-FF4D-7960C68FD000}

Could you try updating to 3.0.25 and see if it fixes the problem?

Thanks, Mark

But still doesn’t work
when I retrieve the posts which has a list of comments, every commentedUser property in the list is null
PS: I didn’t map the Users table

Sherif,

what is your app id?

Regards,
Mark

6CC67C14-0E25-C794-FF4B-07E445FD5100

I see what’s going on now. Your Users table has the following columns which also point to Users:

    followings followers
For both columns you have the "auto-load" enabled. As a result. when users are being retrieved you end up with a circular reference which is not resolved on the client side. Generally speaking the "auto-load" option should not be used in this kind of scenario (if ever in a production ready app). The auto-load will be severely impact performance of your app once you have a large number of objects in your data tables. To work around this problem, I recommend you to do the following:
    Turn off auto-load Use 2-step relation retrieval: https://backendless.com/documentation/data/android/data_relations_retrieve.htm
Regards, Mark

Thanks for your quick response, I really appreciate that

I removed the auto-load from Users table and I commented all the code that needs the followers and followings table columns ,and it works.
Thank you

Is there a value for the “postedUser” in the Post object?

yes and it’s solved now

Are you getting “commentedUser” objects now? What was the root cause of the problem?

Yes, now the commentedUser is returned

I think the root of the problem was in the loopy-return of Users.when I return a user it gets in the loop of getting the followings and followers, and each of users in those columns gets their followings and followers and so on, and that’s an infinite loop. That’s why the server didn’t return a value because it’s working in an infinite loop

I will mark the issue as Solved. However, I think your assessment of the problem is a bit off - the server does returns a result since you were getting a collection of Comment objects. The issue was that the client SDK was not able to reconstruct the hierarchy of the user objects due to the complex object tree.