I have a JPA entity Foo which can point to one of 2 different entities as parent. This is clarified with @Any/@AnyMetaDef annotations. One other side, the parent entities each point to a @OneToMany List[Foo]. One of the parent entities also happens to point to a single optional instance of the other parent entity.
Here is what they look like:
// Foo
@Any(metaColumn = @Column(name = "parent_type"), optional=false)
@AnyMetaDef(
metaType = "string",
idType = "java.util.UUID",
metaValues = {
@MetaValue(value = "targetOne", targetEntity = TargetOne.class),
@MetaValue(value = "targetTwo", targetEntity = TargetTwo.class),
})
@JoinColumn(name = "parent_id")
private Base parent;
// TargetOne extends Base
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "parent_id", referencedColumnName = "id", insertable = false, updatable = false)
@OrderBy("index ASC")
private List<Foo> targetOneItems;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "target_two_id")
private TargetTwo targetTwo;
// TargetTwo extends Base
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id", referencedColumnName = "id", insertable = false, updatable = false)
@OrderBy("index ASC")
private List<Foo> targetTwoItems;
When I create instances of TargetOne with some Foo children and targetOne.targetTwo=null things work fine.
When I create an instance of TargetOne with some Foo children, holding a TargetTwo with other Foo children, then the saves seem to work fine but once my logic is done and JPA takes over and tries to actually write to the DB (fake Hibernate test db), I get a constraint violation exception:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException:
Referential integrity constraint violation:
"FKDRL80OAM7VU3AT5TBH94NHE1W: PUBLIC.FOOS FOREIGN KEY(PARENT_ID) REFERENCES PUBLIC.TARGETONES(ID) ('e8ef3354-4e43-4308-b4b7-d002c7504aa3')"; SQL statement:
insert into foos (parent_type, parent_id, ...other columns...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(Here e8ef3354-4e43-4308-b4b7-d002c7504aa3 is the TargetTwo ID)
This seems weird because the save()s worked, and inspecting the tree in the debugger it looks fine - all the Foos under the TargetOne point to the TargetOne as parent, and all the Foos under the TargetTwo point to the TargetTwo as parent. So why is it apparently complaining that the TargetTwo's Foos should point to a TargetOne?
NOTE: This question is not the same as the linked "dupe". The stuff in that question already works fine for me. My question is one level deeper. Please unlock.