Vote count:
0
Started with getting a hibernate exception on a "select * from PARENT" type of a query, which said I have more than one entry in the CHILD table with the same [foreign] key, which should be unique. That was a red herring - the CHILD table had no such entries. I wiped the table clean, and moved on to the same error, this time citing ANOTHER_CHILD as an offender. In fact ANOTHER_CHILD only has 1 row...
Here, CHILD table(s) store persistent @Entity(s), which are in a bi-directional @OneToOne relationship with Parent @Entity.
NOTE: PARENT tables has a primary key, PARENT_PK, and there is a PARENT_PK column on all "Children", which points to the PARENT entry, which 'owns' the CHILD.
It was suggested to me that the problem is likely due to the bi-directionality of the relationship(s); and that I should make them uni-directional, if possible. OK, as I don't really need bi-directionality, I changed the annotations and made made relationship(s) uni-directional. Now the "get all" kind of a request works fine - but now updating the Parent, adding new Child, is failing with a ConstraintViolationException: ORA-01400: cannot insert NULL into CHILD.PARTNER_PK
I also found this article; it claims uni-directional relationships are not supported when the the foreign key is in the "child" table, which the object relationship is to be unidirectional from Parent to Child. It's not clear on how to solve it, though.
.....
Code snippets:
This is how the relationship was defined in the Parent:
@OneToOne(mappedBy = "parent",
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
orphanRemoval = true)
private Child child;
And this is what was in the Child:
@OneToOne
@JoinColumn(name="PARTNER_PK")
private Parent parent;
, (+ all the getters/setters).
After attempting the change for uni-directional relationship, Child class doesn't have the Parent at all. Parent class now looks like this:
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "PARENT_PK")
private AddressDO address;
Aucun commentaire:
Enregistrer un commentaire