In a parent-children relationship of a pair of classes, the key in the child class is a compound one with its parent key as one of components. This design seems to be good for data retrieval. I, however, need to make a few twists on the code. The data insertion operations is the follwoings: save the A instance first, get its ID and assign it to the B instances and save the Bs. I are wondering whether or not there is a good approach in the regard so that only one save() would do the job.
Code:
public class A implements Serializable {
private Integer id;
...
private Set<B> bs = new HashSet<B>();
...
// business method
public void addB(B b){
this.bs.add(b);
}
}
public class B implements Serializable {
private BStringCompoundKey pk;
private String l;
...
public B(String langCode, String name) {
this.locale = langCode;
this.name = name;
}
...
public void setPk(A a) {
this.pk = new BStringCompoundKey(a, l);
}
...
@Transient
public String getL() {
return l;
}
}
@Embeddable
public class BStringCompoundKey implements Serializable {
private A a;
private String l;
public BStringCompoundKey(A a, String l) {
this.a = a;
this.l = l;
}
...
}