1

假设我们生活在一个人只能拥有一辆车的世界中(请原谅我的蹩脚示例)

假设我有这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 表吗?

4

1 回答 1

1

如果您删除 Vehicle 表,这将使 UserDetails 表失去其参照完整性,因为车辆列的外键将指向任何地方。从 UserDetails 中删除 fk 约束或车辆列,然后您可以删除所需的表。

于 2012-11-30T09:55:10.873 回答