I have 3 entities interacting with each other as explained below.
Entity_A {
@Id
private String id;
@OneToMany(cascade=CascadeType.ALL, mappedBy="a", orphanRemoval=true)
private List<Entity_B> b;
@OneToMany(cascadeType.ALL, mappedBy="a", orphanRemoval=true)
private List<EntityRelationship_A_B> relationship;
}
@Embeddable
EntityRelationship_A_B_Id {
@Column(name="a_id")
private String a_id;
@Column(name="b_id")
private String b_id;
@Column(name="relationship_id")
private String relationship_id;
}
EntityRelationship_A_B {
@EmbeddedId
private EntityRelationship_A_B_Id id;
@ManyToOne
@JoinColumn(name="a_id", referencedColumn="a_id", insertable=false, updateable=false)
private Entity_A a;
@OneToOne(cascade = CascadeType.ALL, mappedBy="relationship", orphanremoval = true)
private Entity_B b;
}
@Embeddable
Entity_B_Id {
@Column(name="a_id")
private String a_id;
@Column(name="b_id")
private String b_id;
//std methods
}
Entity_B {
@EmbeddedId
private Entity_B_Id id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id", referencedColumn = "a_id", insertable = false, updateable = false)
private Entity_A a;
@OneToOne(fetch = fetchType.LAZY)
@JoinColumn(name = "a_id", referencedColumn="a_id", insertable=false, updateable=false)
@JoinColumn(name="b_id", referencedColumn="b_id", insertable=false, updateable=false)
@JoinColumn(name="relationship_id", referencedColumn="relationship_id", insetable=false, updateable=false)
private EntityRelationship_A_B relationship;
// ....
}
ServiceImpl {
save (...) {
Manually populate and set Entity_B_Id into individual instance_A.instance_B
Manually populate and set EntityRelationship_A_B_Id into individual instance_A.instance_A_B
//...finally save the top level entity...
}
}
Due to the nature of data, same instance of Entity_B
can't be related to Both Entity_A
and EntityRelationship_A_B
.
With bi-directional references being implemented for owning sides, I can see EntityRelationship_A_B
saves with proper values for a_id, b_id, relationship_id
;
But Entity_B
always gets stored with null
for relationship_id
; in fact the insert statement for Entity_B
never contains relationship_id
column at all. However I can guarantee by looking into the runtime values that the EntityRelationship_A_B
references are properly set EntityRelationship_A_B.Entity_B
.
What am I doing wrong?