gpt4 book ai didi

java - Hibernate一对一关系错误

转载 作者:行者123 更新时间:2023-12-01 12:48:25 25 4
gpt4 key购买 nike

我有这种关系

    @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;

在数据库中我声明我的 FK

                <addForeignKeyConstraint baseColumnNames="candidacy_id" baseTableName="inform_candidacy" constraintName="fk_candidacy_inform_candidacy"   onDelete="CASCADE" onUpdate="CASCADE" referencedColumnNames="id" referencedTableName="candidacy"/>

但是当我尝试坚持级联 Candidacy 并保存第一次在数据库中尚不存在的 InformCandidacy 时,它们之间的关系尚未创建,并且 InformCandidacy 对于 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);
}
}

在我的 InformCandidacy 表中,我与 Candidacy 没有关系

       *************************** 1. row ***************************
id: 101
candidacy_id: NULL <--------- WTF!
selected_number: NULL
information_letter: NULL
selection_decision: NULL
selectionReport: 2014-06-28

我做错了什么。

问候。

最佳答案

您的映射告诉 hibernate 不要插入或更新“candidacy_id”列(insertable = false,updatable = false)。试试这个方法:

@OneToOne
@JoinColumn(name = "candidacy_id", nullable = false)
@Getter
@Setter
private Candidacy candidacy;

关于java - Hibernate一对一关系错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com