Reputation: 1404
I was wondering if spring data for MongoDB could handle multiple databases and perform cross database queries and inserts.
for example if I want to store EntityA
in DB dbA
and EntityB
in dbB
and EntityA
has a reference to EntityB
, will Spring Data generate the correct DBRef
pointing to the correct collection and the correct database ?
Will I then be able to query EntityA
and then eventually lazy fetch EntityB
?
Morphia lacks this functionality alongside other things, and I was wondering if Spring data had it before making the big dive and ditching Morphia.
Upvotes: 0
Views: 698
Reputation: 83171
The DbRef
annotation has a db
attribute so that you can define the database the reference will be stored in. So assuming a model like this:
class EntityA {
@DbRef(db = "dbB") EntityB entityB;
}
class EntityB { … }
interface ARepository extends Repository<EntityA, Long> { … }
interface BRepository extends Repository<EntityB, Long> { … }
you're client code should look something like this:
EntityB b = new EntityB(…);
EntityA a = new EntityA(…);
a.setB(b);
// store A manually first
aRepository.save(a);
bRepository.save(b);
Upvotes: 1