[英]Unable to save an entity "a foreign key constraint fails "
I am trying to save an entity to a table which was created using the @JoinTable annotation.我正在尝试将实体保存到使用 @JoinTable 注释创建的表中。
This is the main entity where the table is set:这是设置表的主要实体:
public class MachineGroup {
@Id
@GeneratedValue(strategy= GenerationType.AUTO, generator = "machine_groups_seq")
@SequenceGenerator(name = "machine_groups_seq", allocationSize = 1, initialValue = 2)
@Column(name = "id")
private long id;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "machine_groups_to_machines",
joinColumns = @JoinColumn(name = "machine_group_id"),
inverseJoinColumns = @JoinColumn(name = "machine_id"))
private Set<Machine> machines;
}
The Entity I try to save:我尝试保存的实体:
@Entity
@Table(name="machine_groups_to_machines")
@Getter
@Setter
public class MachineGroupToMachine {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@ManyToOne
@JoinColumn(name = "machine_id")
private Machine machine;
@ManyToOne
@JoinColumn(name = "machine_group_id")
private MachineGroup machineGroup;
public MachineGroupToMachine(Machine machine, MachineGroup machineGroup) {
this.machine = machine;
this.machineGroup = machineGroup;
}
}
Now I am trying to save MachineGroupToMachine via the MachineGroupToMachineRepository directly using this:现在我试图通过 MachineGroupToMachineRepository 直接使用这个保存 MachineGroupToMachine :
public MachineGroup save(Machine machine, MachineGroup machineGroup){
Optional<MachineGroupToMachine> omgm = machineGroupToMachineRepository.findMachineGroupToMachineByMachineAndMachineGroup(machine, machineGroup);
if(omgm.isPresent()){
String names = MessageFormat.format("Machine {0} and Machine Group {1}", machine.getName(), machineGroup.getName());
throw new EntityAlreadyExistsException(names);
}
return machineGroupToMachineRepository.save(new MachineGroupToMachine(machine, machineGroup)).getMachineGroup();
}
Hibernate tries to perform this query: Hibernate 尝试执行此查询:
Hibernate: insert into machine_groups_to_machines (machine_id, machine_group_id) values (?, ?)
And this is the exception:这是例外:
Cannot add or update a child row: a foreign key constraint fails (`test_db`.`machine_groups_to_machines`, CONSTRAINT `FK7g1k7n6hssjrwqadtrx0pc840` FOREIGN KEY (`MACHINE_ID`) REFERENCES `machines` (`id`))
Am I saving the entity in a proper manner?我是否以适当的方式保存实体? Should it be done from somewhere else perhaps?
也许应该从其他地方完成?
What is it that you don't understand?你不明白的是什么? You are referring to a machine in this
MachineGroupToMachine
entity that does not exist (yet?) on the database.您指的是此
MachineGroupToMachine
实体中的一台机器,该机器在数据库中不存在(还?)。 If you want to also persist the machine, then you will have to use @ManyToOne(cascade = PERSIST)
on the machine association, but I doubt this is what you generally want.如果你还想保留机器,那么你将不得不在机器关联上使用
@ManyToOne(cascade = PERSIST)
,但我怀疑这是你通常想要的。 So just persist the machine before you persist the MachineGroupToMachine
.所以在你坚持
MachineGroupToMachine
之前坚持机器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.