java - Spring data jpa save -


how can pass test? (was working before migrate code use repositories). bs stored in database after save, object not updated. have achieve it?

given these classes:

@entity public class {    @id    private string id;    @onetomany(fetch = fetchtype.eager, cascade = cascadetype.all)    @joincolumn(name = "aid")    private set<b> bs= new hashset<b>();    ... }  @entity public class b {    @id    @generatedvalue    private int id;    private string aid;    private string foo;    ... } 

and repository:

@repositorydefinition(domainclass = a.class, idclass = string.class) public interface arepository {    ...    void save(a a);    ... } 

this test fail:

    // "a" saved , flushed     b b = new b();     b.setaid(a.getid());     a.getbs().add(b);     arepository.save(a);     asserttrue(b.getid() > 0); 

repository.save() persist (if provided argument transient) or merge (otherwise).

since a not transient, merge performed, meaning there no persist operation cascaded bs.

you either have save b explicitly or add b new a before a saved, persist cascaded properly.


Comments