java - JPA/Hibernate fetch -


i have 2 classes onetomany one-directional relationship.

 class {     @column(name = "a_id")     integer id;     @onetomany     @joincolumn(name = "a_id")     private list<b> listofb;     //getters , setters }  class b {     integer id;     string name;     @column("a_id")     integer aid;     //getters , setters } 

in database have saved instance of b. need do:

a.listofb.add(b); save(a); 

in object b in database have id , name, fk null. need update fk, before need fetch object b; how can it?

only writing custom method, or hibernate/jpa have own method fetch objects?

your mapping not common. common way have @joincolumn in b , mappedby = "a" in a. , can specify cascade. in simplest case cascade = cascadetype.all.

    class {         @column(name = "a_id")         integer id;          @onetomany(mappedby = "a", cascade = cascadetype.all)         private list<b> listofb = new arraylist<b>;          public void addb(b b) {            b.seta(this);             listofb.add(b);         }     }      class b {         integer id;         string name;          @manytoone(fetch = fetchtype.lazy)         @joincolumn(name = "a_id")         private a;     }    

to solve issue

save(a); b.seta(a); saveorupdate(b); 

and cascade same way of addb() method

a.addb(b); save(a); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -