One User, three relation columns to other users, retrieving all related users

I have a user with relations to other users, each referenced by a unique relation column

    friends 1:N colleagues 1:N relatives 1:N
Each friend, colleague, and relative has "profilePic" column pointing to a "Picture" Table. I would like to load the users friends, colleagues and relatives with their profilePics. So far what am able to is load the users friends using a whereclause, with profilePic as follows
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause("Users[friends]" + ".objectId='" + userObjectId + "'");
queryBuilder.setRelated("profilePic");



Backendless.Data.of(BackendlessUser.class).find(queryBuilder, userscallback);

I am unable to chain the “colleagues” and “relatives” to the above query to get all of them in one call

The below does’nt work

String whereClause = "Users[friends]" +
 ".objectId='" + userObjectId + "'" +
 "Users[friends].profilePic.objectId =" + userObjectId + "'" +
 "Users[colleagues]" +
 ".objectId='" + currentUserObjectId + "'" +
 "Users[colleagues].profilePic.objectId =" + userObjectId + "'" +
 "Users[relatives]" +
 ".objectId='" + uerObjectId + "'" +
 "Users[relatives].profilePic.objectId =" + userObjectId;



 DataQueryBuilder queryBuilder = DataQueryBuilder.create();
 queryBuilder.setWhereClause(whereClause);

I have exhausted my server code limit for now to have one call call from the client and three in the backend.

Please assist,

George.

The file columns are not actually relations but simple string properties, so you don’t need to specify them in setRelated(). This said, you only need to load friends, colleagues and relatives relations to a parent user object. Which is done with the following code:

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause("objectId='" + userObjectId + "'");
queryBuilder.setRelated("friends", "colleagues", "relatives");

Backendless.Data.of(BackendlessUser.class).find(queryBuilder, userscallback);

Or even shorter, considering you only need one user by its ID:

Backendless.Data.of( BackendlessUser.class ).findById( userObjectId, Arrays.asList("friends", "colleagues", "relatives"), userscallback )

The friends, colleagues, relatives each have child objects referenced by a column “profilePic”, not set to autoload which i want to load with alongside.

So would this work?

Backendless.Data.of( BackendlessUser.class ).findById( userObjectId, Arrays.asList("friends.profilePic", "colleagues.profilePic", "relatives.profilePic"), userscallback )

Oh sorry, I thought proficePic is a relation to File.
Yes, in case it’s a table, you can use the query you provided: subrelations are specified with a dot, like “friends.profilePic” etc.

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause("objectId='" + userObjectId + "'");
queryBuilder.setRelated("friends.profilePic", "colleagues.profilePic", "relatives.profilePic");
Backendless.Data.of(BackendlessUser.class).find(queryBuilder, userscallback);

Am getting Error-Query contains invalid object related properties with the above

Could you please provide your app ID so that I could test?



Sorry Sergey, please hold

Ok, i was substituting some incorect values,

But still this

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause("objectId='" + userObjectId + "'");
queryBuilder.setRelated("friends.profilePic", "colleagues.profilePic", "relatives.profilePic");
Backendless.Data.of(BackendlessUser.class).find(queryBuilder, userscallback);

is giving me the user whose objectId i have specified in the whereClause as a single object, i bet with the friends, colleagues and relatives relations.

What i want is the query to return a list of friends, colleagues and relatives in handleResponse, so i don’t have to write code to get the friends, colleagues and relatives objects from the user object.

Is there a way to tweek the query to do that? friends, colleagues and reatives are also users in the same table.

Regards,
George

Essentially what you’re trying to do is to merge multiple different query results into one - and no, it’s not possible using a single query. Imagine if all these relations pointed to different tables - then you’d need one collection of three different types, which is impossible.

So in your case you need to just merge the relations in your code, which is actually quite simple:

public void handleResponse( BackendlessUser parentUser ) { 
List<BackendlessUser> friendsAndColleaguesAndRelatives = new ArrayList<>(); friendsAndColleaguesAndRelatives.addAll( (Collection<BackendlessUser>) parentUser.getProperty( "friends" ); 
friendsAndColleaguesAndRelatives.addAll( (Collection<BackendlessUser>) parentUser.getProperty( "colleagues" ); 
friendsAndColleaguesAndRelatives.addAll( (Collection<BackendlessUser>) parentUser.getProperty( "relatives" ); 
// work with merged collection
}

I get it, thanks Sergey.