schema - Spring mongodb how to use crudRespository with embedded documents -
why when created crudrespository search on embedded object none of queries work , of them return null. there i'm doing wrong? there example. hope not limitation of spring mongodb. maybe know different way extract embedded object? use @dbref read not recommended performance issues.
country.class
@document(collection="countries") @typealias("ctry") public class country { @id private biginteger id; private string name; private continent continent; public country(string name, continent continent) { setname(name); setcontinent(continent); } //get/set omitted }
embedded document continent.class
public final class continent { private final long id; private final string name; public continent(long id, string name) { this.id = id; this.name = name; } //get/set omitted }
continentrespository.class - works fine data returned should
public interface countryrepository extends repository<country, biginteger> { //------------------------------------------- equality public country findbyname(string countryname); @query("{name : ?0}") public country findbynamequery(string countryname); }
continentrespository.class - methods return null :/
public interface continentrepository extends crudrepository<continent, long> { public continent findbyname(string name); @query("{name : ?0}") public continent findbynamequery(string name); }
respositorytest.class - test class testfindembeddedcontinent fails because returned object null. testfinddocumentcountry works expected
@contextconfiguration(classes={mongoconfig.class}) @runwith(springjunit4classrunner.class) public class repositorytest { @autowired private countryrepository countryrepository; @autowired private continentrepository continentrepo; @autowired private mongooperations mongoops; @autowired private mongotemplate template; @before public void reset() { dbcollection countries = template.getcollection("countries"); countries.drop(); countries.insert(dbobjectfromjson("{ '_id' : '8', '_class' : 'ctry', 'name' : 'japan', 'continent' : { '_id' : 2, 'name' : 'asia' } }")); } @test public void testfindembeddedcontinent() { continent asia = continentrepo.findbyname("asia"); assertnotnull(asia); assertthat(asia.getname(), is(equalto("asia"))); } @test public void testfinddocumentcountry() { country japan = countryrepository.findbyname("japan"); assertnotnull(japan); assertthat(japan.getcontinent().getname(), is(equalto("asia"))); assertthat(japan.getname(), is(equalto("japan"))); } private static dbobject dbobjectfromjson(string json) { return (dbobject) json.parse(json); } }
a repository - definition - mimics collection of aggregate roots. in mongodb translates top-level documents of collection. thus, can create repositories top-level documents, i.e. country
in case.
im not totally sure makes sense continent
property of country
(as country part of continent) might due sample. if want query continents
independently, make them top-level document , keep references country either using simple id or dbref (the former recommended).
Comments
Post a Comment