We have a requirement to insert to DB using Hibernate which has a Data model as below
@javax.persistence.Entity(name = "table1")
@SecondaryTable(name = "table2", pkJoinColumns = @PrimaryKeyJoinColumn(name = "fk2ObjId", referencedColumnName = "object_id"))
public class Parent {
}
@SecondaryTable(name = "table2", pkJoinColumns = @PrimaryKeyJoinColumn(name = "fk2ObjId", referencedColumnName = "object_id"))
public class Child extends Parent {
@ManyToOne(optional = true, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinColumn(table = "table2", name = "fk2RefId", nullable = true)
private Reference reference = null;
}
Now when I try to insert data to table2
, I see insert statements as below
- insert into table1 – from Parent class
- insert into table2 – from Parent class
- insert into table2 – from child class
Unfortunately, as the fk2ObjId
is the primary key, the insert at 2 and 3 will conflict for unique constraint violation which is obvious
Question: How let Child class know not to generate another insert statement and update the existing one?