Manttomany-无法添加或更新子行:外键约束失败
我正在尝试创建许多与许多关系,并将一些数据保存到数据库中。 我有实体:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以这种方式映射您的实体
Try to map you entity like this way