Manttomany-无法添加或更新子行:外键约束失败

发布于 2025-01-25 02:30:33 字数 1641 浏览 3 评论 0原文

我正在尝试创建许多与许多关系,并将一些数据保存到数据库中。 我有实体:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class UnifiedOfferEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String idOffer;
    private String companyName;
    private String city;
    private String street;
    private String title;
    private LocalDateTime posted;
    private String url;
    private boolean remote;
    private boolean remoteRecruitment;
    @ManyToMany
    @JoinTable(
        name = "terms_of_contract",
        joinColumns = @JoinColumn(name = "offer_id"),
        inverseJoinColumns = @JoinColumn(name = "contract_id"))
    private Set<ContractDetailsEntity> contractDetails = new HashSet<>();
}

@Entity
@Data
@Table(name = "ContractDetails")
@AllArgsConstructor
@NoArgsConstructor
public class ContractDetailsEntity {
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    @Enumerated(EnumType.STRING)
    private TypeOfContract typeOfContract;
    private double salaryFrom;
    private double salaryTo;
    @ManyToMany(mappedBy = "contractDetails")
    private Set<UnifiedOfferEntity> unifiedOffers = new HashSet<>();

}

,当我想保存一些东西时,我会得到执行,我不知道怎么了:

java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`jobfinder`.`terms_of_contract`, CONSTRAINT `FKfyi46wtjxm9cmb5fh28lf47qt` FOREIGN KEY (`contract_id`) REFERENCES `contract_details` (`id`))

I am trying to create Many to Many relationship and save some data to database.
I have entities:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class UnifiedOfferEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String idOffer;
    private String companyName;
    private String city;
    private String street;
    private String title;
    private LocalDateTime posted;
    private String url;
    private boolean remote;
    private boolean remoteRecruitment;
    @ManyToMany
    @JoinTable(
        name = "terms_of_contract",
        joinColumns = @JoinColumn(name = "offer_id"),
        inverseJoinColumns = @JoinColumn(name = "contract_id"))
    private Set<ContractDetailsEntity> contractDetails = new HashSet<>();
}

@Entity
@Data
@Table(name = "ContractDetails")
@AllArgsConstructor
@NoArgsConstructor
public class ContractDetailsEntity {
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    @Enumerated(EnumType.STRING)
    private TypeOfContract typeOfContract;
    private double salaryFrom;
    private double salaryTo;
    @ManyToMany(mappedBy = "contractDetails")
    private Set<UnifiedOfferEntity> unifiedOffers = new HashSet<>();

}

And when I want to save something I am getting that execption and I do not know what is wrong:

java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`jobfinder`.`terms_of_contract`, CONSTRAINT `FKfyi46wtjxm9cmb5fh28lf47qt` FOREIGN KEY (`contract_id`) REFERENCES `contract_details` (`id`))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2025-02-01 02:30:33

尝试以这种方式映射您的实体

  @ManyToMany(cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
    })
    @JoinTable(
        name = "terms_of_contract",
        joinColumns = @JoinColumn(name = "offer_id"),
        inverseJoinColumns = @JoinColumn(name = "contract_id"))
    private Set<ContractDetailsEntity> contractDetails = new HashSet<>();

Try to map you entity like this way

  @ManyToMany(cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
    })
    @JoinTable(
        name = "terms_of_contract",
        joinColumns = @JoinColumn(name = "offer_id"),
        inverseJoinColumns = @JoinColumn(name = "contract_id"))
    private Set<ContractDetailsEntity> contractDetails = new HashSet<>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文