簡體   English   中英

休眠關系外鍵約束失敗

[英]Hibernate relationship foreign key constraint fails

為了了解如何使用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自動生成的,所以無法保存汽車? 那正確嗎?

你這里有幾個問題。 在Car中,您有一個字段,該字段應該是對Showroom的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引用替換它(請參見上文)。

另一個問題是您生成的Column是本機類型。 這將成為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;
}

為了從數據庫成功加載實體,我還必須為Car添加默認的構造函數。

protected Car() {
}

然后您的示例將起作用。

我設法做到了這一點。 第一個問題是我的汽車課上沒有陳列室。 第二個是我保存對象的方式似乎不正確。

我已經改變了班級。

@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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM