java - spring jpa - how to persist transitive bidirectional relationship -


i have 3 classes (a, b, c) have onetomany relationships a <>-- b <>-- c code:

@getter @setter @entity public class {     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @onetomany(fetch = fetchtype.eager, mappedby = "a")     private list<b> blist;      private string name; }  @getter @setter @entity public class b {     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @manytoone(fetch = fetchtype.eager)     private a;      @onetomany(fetch = fetchtype.eager, mappedby = "b")     private list<c> clist;      private string name; }  @getter @setter @entity public class c {     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      private string name;      @manytoone(fetch = fetchtype.eager)     private b b; } 

how should populate data? when i'm trying this:

    a = new a();     a.setname("a-1");     arepository.save(a);      b b1 = new b();     b1.setname("b-1");     b1.seta(a);     brepository.save(b1);      b b2 = new b();     b2.setname("b-2");     b2.seta(a);     brepository.save(b2);      (int = 1; <= 9; i++ ) {         c c = new c();         c.setname("c-"+i);         c.setb(b1);         crepository.save(c);     } 

i populated data in database:

+----+------+ | id | name | +----+------+ | 1  | a-1  | +----+------+  +----+------+------+ | id | name | a_id | +----+------+------+ | 1  | b-1  | 1    | +----+------+------+ | 2  | b-2  | 1    | +----+------+------+  +----+------+------+ | id | name | b_id | +----+------+------+ | 1  | c-1  | 1    | +----+------+------+ | 2  | c-2  | 1    | +----+------+------+ | 3  | c-3  | 1    | +----+------+------+ | 4  | c-4  | 1    | +----+------+------+ | 5  | c-5  | 1    | +----+------+------+ | 6  | c-6  | 1    | +----+------+------+ | 7  | c-7  | 1    | +----+------+------+ | 8  | c-8  | 1    | +----+------+------+ | 9  | c-9  | 1    | +----+------+------+ 

but when i'm trying fetch data repository wrong:

these tests ok:

    assertthat(crepository.findone(1l)).isnotnull();     assertthat(crepository.findone(1l).getb()).isnotnull();     assertthat(crepository.findall()).hassize(9);     assertthat(brepository.findall()).hassize(2);     assertthat(brepository.findone(1l).getclist().size()).isequalto(9); 

but 1 failing:

    assertthat(arepository.findone(1l).getblist().size()).isequalto(2); 

it returns 10 records. query select * b a_id = 1 returns 2 records, please shed light on doing wrong?

its strange hibernate behaviour results fact eager type using outer joins. duplicate b objects each of c objects related b. had similiar problem , solution found change fetchtype lazy or change list set.

here have better explanation: hibernate criteria returns children multiple times fetchtype.eager or duplicates in onetomany annotated list


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -