java - Eclipselink insert joined object with @OneToOne -
i have 2 objects: primary key a_id, , b primary key b_id. b has foreign key a_id. while @ database level means have multiple b objects point same a, relationship 1 one b. both a_id , b_id generated sequences. have following eclipselink annotations describe relationship:
public class { ... @onetoone(mappedby = "a", fetch = fetchtype.lazy, cascade = cascadetype.all, targetentity = b.class) public b getb(); ... } public class b { ... @onetoone(fetch = fetchtype.lazy, targetentity = a.class) @joincolumn(name = "a_id") public geta(); ... }
this doesn't work. if have object, b object set on it, , b object has same object set on it, b object not inserted, when a.getb() null. however, if change annotation on @onetomany so:
public class { ... @onetomany(mappedby = "a", fetch = fetchtype.lazy, cascade = cascadetype.all, targetentity = b.class) public set<b> getb(); ... }
with else same, inserts b, without a_id set linking object. merge logic (for both cases) boils down to:
public void merge(a obja, b objb) { obja.setb(objb); objb.seta(obja); entitymanager.merge(obja); }
i asked this question case both objects share same id. difference in question besides shared id (which leads @mapsid annotation) b object has cascade = cascadetype.all
part of annotation, tried in scenario , still fails. have idea how case separate id b object work using @onetoone?
Comments
Post a Comment