Reputation: 1291
How can I work with complex types in Spring Data + Mongo?
Like:
class Person {
@Id
String id;
String name;
//What can I do?
Address address
//OR
String adressId;
}
How can I build this object for server side use?
I want to use adressId, but I don't know how to proceed when I need to use the address within some method.
For example:
void doWithPerson(Person person){
System.out.println(person.getAdress());//this doesn't exist with adressId
}
Edit:
I want the mongo object as:
{
id: 1
name: 'Test'
addressId: 1//not the complext object
}
and in addressCollection:
{
id: 1
address: 'Some info'
}
Upvotes: 0
Views: 425
Reputation: 30839
You can use @DBRef
annotation to store reference of another object into your class object, e.g.:
@DBRef(lazy = true)
Address address;
This way, you can find the Person
with particular address id. You can also retrieve Address
objects independently by using mongo repository
for Address
class.
Here is the documentation.
Upvotes: 1