[英]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.