假设我们生活在一个人只能拥有一辆车的世界中(请原谅我的蹩脚示例)
假设我有这UserDetails
门课
public class UserDetails {
@Id
@GeneratedValue
@Column(name = "USER_ID")
private int id;
@Column(name = "USER_NAME")
private String name;
@OneToOne
private Vehicle vehicle;
public Vehicle getVehicle() {
return vehicle;
}
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
这是我的Vehicle
课
@Entity
public class Vehicle {
@Id
@GeneratedValue
private long vechile_id;
private String vehicleName;
public long getVechile_id() {
return vechile_id;
}
public void setVechile_id(long vechile_id) {
this.vechile_id = vechile_id;
}
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
}
将其保存到数据库后它工作正常,但是当我去删除车辆表时,这个错误出现在我的工作台上
注意UserDetails
和表上只有一个条目Vehicle
。
ERROR 1217: Cannot delete or update a parent row: a foreign key constraint fails
SQL Statement:
drop table `hibernate`.`vehicle`
怎么不让我掉表?我应该先删除 UserDetails 表吗?