Spring Data MongoDB Index not created when no data available -
i using spring data framework mongodb groovy , can't solve problem: want: want spring data create mongodb collection 2 indices. have pojo class
@document(collection='language') class language { @id string id @indexed(unique = true) string isocode string description }
that repository:
public interface languagerepository extends mongorepository<language, string> list<language> findbyisocode(string isocode); }
maybe there more annotations, writing code without having sourcecode right here...
i have written spock test checks if possible insert 2 languages same isocode , expects exceptoion. spock test wires application context , works pretty fine except of 1 point: index not created when run test first time.
i wire test, test inserts twi languages same isocode mongodb , test fails because there no index prevents duplicate isocodes. can see in mongoshell well. test fails because there no index violation , no exception. when run same test again application can't start because spring data seems create index on mongodb - fails, because there duplicate entries in database , creation of index fails (of course...). when delete 1 of entries manually via mongoshell , run same test again works fine, because index can created , expected exception.
can tell me if , why mongodb can create indices if there entries in collection? , if way: how handle issue best?
kind regards sebastian
edit spock test:
def 'there must not duplicate languages'() { setup: language lang1 = new language( isocode: 'de-de', description: 'deutsch (deutschland)' ) language lang2 = new language( isocode: 'de-de', description: 'deutsch (schweiz)' ) languagerepository.save(lang1) when: languagerepository.save(lang2) then: final org.springframework.dao.duplicatekeyexception ex = thrown() cleanup: languagerepository.deleteall() }
Comments
Post a Comment