Hibernate关系外键约束失败

时间:2015-11-13 20:50:13

标签: java hibernate

我已经将两个基本类/表放在一起,以便了解如何使用Hibernate。

执行以下代码后;

Session hbSession = HibernateUtil.getSession();

        Showroom showroom = new Showroom();
        showroom.setLocation("London");
        showroom.setManager("John Doe");

        List<Car> cars = new ArrayList<Car>();
        cars.add(new Car("Vauxhall Astra", "White"));
        cars.add(new Car("Nissan Juke", "Red"));

        showroom.setCars(cars);

        hbSession.beginTransaction();
        hbSession.save(showroom);
        hbSession.getTransaction().commit();

我收到此错误;

Cannot add or update a child row: a foreign key constraint fails (`ticket`.`Car`, CONSTRAINT `FK107B4D9254CE5` FOREIGN KEY (`showroomId`) REFERENCES `Showroom` (`id`))

我不确定它出了什么问题。以下是两个带注释的类;

@Entity
public class Showroom {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToMany
    @JoinColumn(name="showroomId")
    @Cascade(CascadeType.ALL)
    private List<Car> cars = null;

    private String manager = null;
    private String location = null;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

@Entity
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private String color;
    private int showroomId;

    public Car(String name, String color) {
        this.setName(name);
        this.setColor(color);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getShowroomId() {
        return showroomId;
    }

    public void setShowroomId(int showroomId) {
        this.showroomId = showroomId;
    }
}

目前我已让Hibernate在MySQL数据库中创建表。我已经检查过,Car表中确实存在数据库之间的关系。

有人能告诉我为什么这不起作用吗?

我猜是因为陈列室没有Id,因为这是由MySQL自动生成的,所以汽车无法保存?是吗?

2 个答案:

答案 0 :(得分:1)

这里有几个问题。 在Car中,你有一个字段应该是展厅的FK参考。但是,这是一个原生的int。这意味着它的值为零。 如果您的Car对象应该引用您的Showroom,那么您必须使用@ManyToOne添加引用

@ManyToOne
@JoinColumn(name="showroomId")
private Showroom showroom;

然后,您在Showroom中的字段变为

@OneToMany(mappedBy = "showroom")
@Cascade(value = { org.hibernate.annotations.CascadeType.ALL } )
private List<Car> cars = null;

如果这样做,您需要明确地在Car中设置引用。 无论哪种方式,showroomId(也是本地int)都需要去。您根本不应该在Car对象中包含该字段,并且您只有backref List,或者您需要将其替换为正确映射的Entity引用(参见上文)。

另一个问题是您生成的列是本机类型。这变为值0,Hibernate将不会自动/正确地生成值。

更改两个实体中的主键引用(以及getter和setter)

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

为了从DB成功加载实体,我还必须为Car添加默认构造函数。

protected Car() {
}

然后你的例子就可以了。

答案 1 :(得分:0)

我设法让这个工作。第一个问题是我在我的汽车课上没有陈列室房产。第二个是我保存对象的方式似乎不正确。

我已经改变了我的课程..

@Entity
public class Showroom {

    @Id
    @GeneratedValue
    private int id;

    private String location;
    private String manager;

    @OneToMany(mappedBy="showroom")
    private List<Car> cars;

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
}

@Entity
public class Car {

    @Id
    @GeneratedValue
    private int Id;

    private String name;
    private String color;

    @ManyToOne
    @JoinColumn(name="showroomId")
    private Showroom showroom;

    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
}

现在保存功能;

Session hbSession = HibernateUtil.getSession();
        hbSession.beginTransaction();

        // Create a showroom
        Showroom showroom = new Showroom();
        showroom.setManager("John Doe");
        showroom.setLocation("London");
        hbSession.save(showroom);

        // Create car one, assign the showroom and save
        Car car1 = new Car("Vauxhall Astra", "White");
        car1.setShowroom(showroom);
        hbSession.save(car1);

        // Create car two, assign the showroom and save
        Car car2 = new Car("Nissan Juke", "Red");
        car2.setShowroom(showroom);
        hbSession.save(car2);

        hbSession.getTransaction().commit();