java - cascade save in hibernate, foregin key is saved as null -
i have class follows
class a{ set<b> = new hashset<b>(); } class b{ a; }
now primary key of autogenerated cant set before in or b.b has inverse mapping of a.and object within b null originally. maps 2 tables , b in db.now if have object set contains 2 records when save object 2 records gets created in b.now code working fine.but when see recods in b find foreign key corresponding blank.how come?
table b structure bid aid bname
//aid foreign key tablles primary key , aid stored null, why so?i want aid should automatically stored in b
you need correctly set both side of relation
parenta.getbchilds().add(childb); childb.setparenta(parenta); <-- important see link below working bi
you can create link management methods in parent correctly set both sides.
public class parenta { ... public void addchildb(b pchild) { this.childsb.add(pchild); pchild.setparenta( ); } ... }
set 'inverse' attribute true on collection relation
<set name="bchilds" inverse="true"> <key column="bid"/> <one-to-many class="childb"/> </set>
maybe you're mixing between inverse , cascade. explanation can read blog entry on hibernate inverse attribute hibernates bizarre interpretation of inverse ;) , hibernate doc on working bi-directional links
Comments
Post a Comment