Read Document with key annotated by @DBRef

43 Views Asked by At

For the past five days I have been struggling with the problem of reading documents from MongoDB collections.

Here is my User model:

@Data
@Document(collection = "user")
public class User {

    @Id
    private ObjectId userId;

    @DBRef
    private List<Role> roleId;

    private String login;

}

And Role model:

@Data
@Document(collection = "role")
public class Role {

    @Id
    private ObjectId roleId;

    private ObjectId tenantId;

    private String role;

}

After adding 3 documents to the role's collection, and the document to the user's collection, the roleId key in user's collection contains the following: [{"$ref":"role","$id":"65d5f9201aed180eeb093a07"},{"$ref":"role","$id":"65d5f9201aed180eeb093a09"},{"$ref":"role","$id":"65d5f9201aed180eeb093a0b"}]

Then im trying to read users from the collection and getting error: java.lang.UnsupportedOperationException: DBRef resolution is not supported

That's how I read:

List<User> users = userRepository.findAll();

Here is the UserRepository:

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends MongoRepository<User, ObjectId> {

}

I suspect that the value from the roleId key is not comparable to the private List<Role> roleId, but I can't figure out how I read users correctly.

I expect correct reading of users whose roleId value in the User model is annotated by @DBRef

0

There are 0 best solutions below