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<Map> 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?
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
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.