hibernate - How to map one to many relationship with itself -


i have table called territory has id, name, id_parent_territory in it. in 1 many territory_with_subterritories. both columns in territory_with_subterritories self foreign key territory(self references). stores rows this:

 territory:  id      name        id_parent_territory ---    ------     --------------------- 1      india        null  2      karnataka    1  3      bangalore    2 
 territory_with_subterritories:  id_parent_territory      id_subterritory ---------------------    ------------------  1                          2  2                          3  

i'm not able save data territory_with_subterritories when data saved territory.

the join table i've written is:

@onetomany(fetch = fetchtype.lazy, targetentity = territorydataimpl.class) @jointable(name = "territory_with_subterritories",             joincolumns = @joincolumn(name = "id_subterritory"),             inversejoincolumns = @joincolumn(name = "id_parent_territory"))         private list<territory> childrenstructures = new arraylist<territory>();` 

after territory collects parentstructure ui, i've following code save:

territory.getparentterritorialstructure().getchildrenstructures().add(object);     entitymanager.merge(territory); 

territorydataimpl:

@entity @table(name = "territory") public class territorydataimpl {    @id   @generatedvalue   @column(name = "id", columndefinition = "serial", nullable = false)   private long id;    @length(max = 50)   @column(name = "name", length = 50)   private string name;    @onetomany(fetch = fetchtype.lazy, targetentity = territorydataimpl.class)   @jointable(name = "territory_with_subterritories",       joincolumns = @joincolumn(name = "id_parent_structure"), inversejoincolumns = @joincolumn(           name = "id_subterritory"))   private list<territory> childrenstructures = new arraylist<territory>();  } 

this worked:

    @entity     @table(name = "territory")     public class territorydataimpl {        @id       @generatedvalue       @column(name = "id", columndefinition = "serial", nullable = false)       private long id;        @length(max = 50)       @column(name = "name", length = 50)       private string name;        @onetoone(fetch = fetchtype.eager, targetentity = territorydataimpl.class)       @column(name="id_parent_territory")       private territorydataimpl parentterritorialstructure;        @onetomany(mappedby = "territorystructure", targetentity = territorydataimpl.class,           orphanremoval = true, cascade = {cascadetype.remove, cascadetype.merge})       private list<territory> childrenstructures = new arraylist<territory>();        @manytoone(fetch = fetchtype.eager, targetentity = territorydataimpl.class)       @jointable(name = "territory_with_subterritories",           joincolumns = @joincolumn(name = "id_parent_structure"), inversejoincolumns = @joincolumn(               name = "id_subterritory"))       private territorydataimpl territorystructure;      } 

after territory collects parentstructure ui, i've following code save:

object.setterritorystructure(object.getparentterritorialstructure()); entitymanager.merge(territory); 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -