Reputation: 13471
I have this relationship
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "candidacy")
@XmlElement
@Getter
@Setter
private InformCandidacy informCandidacy;
@OneToOne
@JoinColumn(name = "candidacy_id", nullable = false, insertable = false, updatable = false)
@Getter
@Setter
private Candidacy candidacy;
On database I declare my FK
<addForeignKeyConstraint baseColumnNames="candidacy_id" baseTableName="inform_candidacy" constraintName="fk_candidacy_inform_candidacy" onDelete="CASCADE" onUpdate="CASCADE" referencedColumnNames="id" referencedTableName="candidacy"/>
But when I try to persist on cascade Candidacy and then save InformCandidacy which first time does not exist on database yet, the relationship between them is not created and InformCandidacy is null for Candidacy
@Override
public void saveAll(final List<Candidacy> candidacies) {
for (Candidacy candidacy : candidacies) {
Candidacy candidacyDB = findById(candidacy.getId());
candidacy.getInformCandidacy().setCandidacy(candidacyDB);
candidacyDB.setInformCandidacy(candidacy.getInformCandidacy());
candidacyRepository.save(candidacyDB);
}
}
and in my table of InformCandidacy I dont have the relationship with Candidacy
*************************** 1. row ***************************
id: 101
candidacy_id: NULL <--------- WTF!
selected_number: NULL
information_letter: NULL
selection_decision: NULL
selectionReport: 2014-06-28
What I´m doing wrong.
Regards.
Upvotes: 0
Views: 53
Reputation: 2262
Your mapping tells hibernate to not insert or update the "candidacy_id" column (insertable = false, updatable = false). Try it this way :
@OneToOne
@JoinColumn(name = "candidacy_id", nullable = false)
@Getter
@Setter
private Candidacy candidacy;
Upvotes: 1