Backendless SDK Data Query

Hi everyone,

i want to write a server side java service which should return all Friends of a User.
The data tables im using are ‘Users’ and ‘Friends’. Friends has the columns “fromUser”, “toUser” and “accepted” where “fromUser” and “toUser” represents one-to-many relations of Users.

With the following code i recieve the desired information BUT i only want to recieve a collection of Users NOT a complete collection of Friends documents where the where clause applies:

public BackendlessCollection<Map> getFriendsFromUser(String objectId) {
StringBuilder whereClause = new StringBuilder();
whereClause.append( “(fromUser.objectId=’” ).append( objectId ).append( “’” );
whereClause.append( " or " );
whereClause.append( “toUser.objectId=’” ).append( objectId ).append( “’” );
whereClause.append( ") and " );
whereClause.append( “accepted=true” );

BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( whereClause.toString() );
BackendlessCollection&lt;Map&gt; user = Backendless.Persistence.of("Friends").find(dataQuery);
return user;

}
Does the Backendless SDK provide the functionality to only return the desired Users in a collection where the where clause applies and which do not have the same objectId like the provided one?

Thanks,
Philip

Hi Philip,

Your problem will be clearer for us if we can see your data scheme.
Please, provide us your app id. You can send it to support@backendless.com.
Regards, Ilya

Philip,
You would like to get all friends of user with objectId = :userId
I can suppose the next solution in three steps:

First step: Make request to Users table to get all items where

Friends[toUser].fromUser.objectId = :userId AND Friends[toUser].accepted = TRUE

Second step: Make request to Users table to get all items where

Friends[fromUser].toUser.objectId = :userId AND Friends[toUser].accepted = TRUE

Third step: Concat this two collections

Please, notice that because of pagination of results you should do find operation recursively to get all users. You can read more about pagination of responses here

I hope it will be helpful for you.
Regards Ilya

Hi,

thanks, but with your provided where clause:

Friends[toUser].fromUser.objectId='objectID' AND Friends[toUser].accepted=TRUE

i get this error :

{
  "code": 1017,
  "message": "Invalid where clause. Specified Entity: Friends[toUser]  is not related to lowerLevelEntity: Friends"
}

Regards,
Philip

Hi Phillip,

The query Ilya wrote about must be sent to the Users table, not Friends (meaning the of() method must reference BackendlessUser.class). Please try that and let us know if it works.

Regards,
Mark

Hi Mark,

thanks this worked out for me, but now i get this error and i dont know why…

{
  "code": 3071,
  "message": "Could not perform persistence operation: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Not unique table/alias: '8309C5B3-E628-B931-F.31B295EC-3840-4FA3-F.14FFC'"
}



this is my code:

public BackendlessCollection&lt;BackendlessUser&gt; getFriendsFromUser(String objectId) {
        StringBuilder whereClause = new StringBuilder();
        whereClause.append( "(Friends[toUser].fromUser.objectId='" ).append( objectId ).append( "'" );
        whereClause.append( " and " );
        whereClause.append( "Friends[toUser].accepted=true)" );
        whereClause.append( " or " );
        whereClause.append( "(Friends[fromUser].fromUser.objectId='" ).append( objectId ).append( "'" );
        whereClause.append( " and " );
        whereClause.append( "Friends[fromUser].accepted=true)" );

        BackendlessDataQuery dataQuery = new BackendlessDataQuery();
        dataQuery.setWhereClause( whereClause.toString() );

        BackendlessCollection&lt;BackendlessUser&gt; user = Backendless.Persistence.of(BackendlessUser.class).find(dataQuery);
        return user;
    }

Thanks,
Philip

Hi Phillip,

Could you please let me know your application ID?

Regards,
Mark

Hi Mark,

everthing is fine now, i just need to do the 2 step aproach which Ilya told me, concat the results and it works out.

Regards,
Philip

Great. I will mark the topic as “Solved” for now.

Regards,
Mark