I have this two documents, User:
@Document(collection = "User")
public class User {
// fields
}
and Contact:
@Document(collection = "Contact")
public class Contact extends User{
// fields
}
and then I have a document which referes either to User oder Contact:
@Document(collection = "DocumentFile")
public class DocumentFile {
@DBRef
private User user;
}
So I am able to add User oder Contact in DocumentFile#user but if I set a Contact to DocumentFile#user than I lost the reference because in MongoDB DocumentFile#user is stored as "_class" : "...Contact". Is there a solution for that?
This is how your classes should look like to make the DBRef work with the inheritance.
User
Contact
Please note you don't need Document annotation on this class.
Document File
You'll just need the
IDocumentFileRepository
andIUserRepository
for CRUD operations.Rest of the code along with the test cases have been uploaded to github.
https://github.com/saagar2000/Spring