I have my java classes as
Class Parent{
@ID @GeneratedValue(strategy=GenerationType.Identity) @Column(id) Int id;
@oneToOne(mapped by=parent, Cascade=CascadeType.All) Child1 c1;
@oneToOne(mapped by=parent,Cascade=CascadeType.All) Child2 c2;
// getter setter
}
Class Child1{
@ID @GeneratedValue(generator = "gen") @GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="parent")) @Column(id) Int id;
@oneToOne @primaryKeyJoinColumn Parent parent;
@oneToMany(mapped by=child1,Cascade=CascadeType.All) List<Toy> toys;
// getter setter
}
Class Child2{
@ID @GeneratedValue(generator = "gen") @GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="parent")) @Column(id) Int id;
@oneToOne @primaryKeyJoinColumn Parent parent;
@oneToMany(mapped by=child2,Cascade=CascadeType.All) List<Toy> toys;
// getter setter
}
Class Toy{
@EmbeddedId ToyPk toyid;
@manytoOne @joinColumn(name="id1") @MapsId("id1") Child1 child1;
@manytoOne @joinColumn(name="id1") @MapsId("id1") Child2 child2
// getter setter }
@Embeddable Class ToyPk{
@column("id1") int id1;
@column("id2") int id2;
//getter setter }
At time of save or retrieve, i can have either Parent-Child1-Toy list or Parent-Child2-Toy list. When i save object with Parent-Child1-Toy list, i get exception= "attempted to assign id from null one to one(TOY.child2)". What should i do?
|