Andrea T
Andrea T

Reputation: 3107

Spring Data integration for ArangoDb gives back empty result list on queries

When I use queries from method names with ArangoDb and Spring Data integration I always get empty lists. What is the problem in the way I use? This is an example.

These are the Entities:

Authorization:

@Document("authorizations")
@Data // (Lombok)
public class Authorization {

  @Id
  private String id;

  private String caption;

  @Relations(edges = com.picktur.server.relations.UserAuthorized.class, lazy = true)
  private User user;

}

User:

@Document("users")
@Data @NoArgsConstructor // (Lombok)
public class User {

  @Id
  private String id;

  private String username;

  @Relations(edges = com.picktur.server.relations.UserAuthorized.class, lazy = true)
  private Collection<Authorization> authorizations;

}

Authorization Repository:

public interface AuthorizationRepo extends ArangoRepository<Authorization, String> {

  Iterable<Authorization> findAllByUser(User user);

}

Edge between User and Authorization:

@Edge
@Data
public class UserAuthorized {

  @Id
  private String id;

  @From
  private User user;

  @To
  private Authorization authorization;

public UserAuthorized( User user, Authorization authorization) {
    this.authorizedPhoto = user;
    this.authorization = authorization;
}

}

Repository for UserAuthorized Egde:

public interface UserAuthorizedRepo extends ArangoRepository<UserAuthorized, String> {

}

Data Persistency logic:

    User user = get_user_from_security_context();
    Authorization authorization = new Authorization();        
    authorization.setUser(user);
    ...
    authorization = authorizationRepo.save(authorization);

    UserAuthorized userAuthorized = new UserAuthorized(user, authorization);
    userAuthorizedRepo.save(userAuthorized);

Query that has empty result:

Iterable<Authorization> authorizations = authorizationRepo.findAllByUser(user);

All the documents and edges exist in DB but the result is empty.

Upvotes: 0

Views: 360

Answers (1)

rashtao
rashtao

Reputation: 667

Query derivation generates AQL query on document fields, eg. in case of findAllByUser it performs an AQL query like:

FOR a IN authorizations FILTER a.user == @user RETURN a

which obviously returns an empty array.

You can check the generated query enabling debug log level globally in your logback.xml.

According to your data model, to fetch all the authorizations of a given user you should perform something like this:

User user = userRepo.findById(userId).get();
Iterable<Authorization> authorizations = user.getAuthorizations();

Upvotes: 1

Related Questions