Java 休眠关系外键约束失败
为了学习如何使用Hibernate,我将两个基本类/表放在一起 在执行以下代码时Java 休眠关系外键约束失败,java,hibernate,Java,Hibernate,为了学习如何使用Hibernate,我将两个基本类/表放在一起 在执行以下代码时 Session hbSession = HibernateUtil.getSession(); Showroom showroom = new Showroom(); showroom.setLocation("London"); showroom.setManager("John Doe"); List<Car> cars = new
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();
我不确定哪里出了问题。下面是两个注释类
@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;
}
}
@实体
公共类陈列室{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私有int-id;
@独身癖
@JoinColumn(name=“showroomId”)
@级联(级联类型.ALL)
私家车列表=空;
私有字符串管理器=null;
私有字符串位置=null;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共列表getCars(){
返回车辆;
}
公共车辆(列出车辆){
这个。汽车=汽车;
}
公共字符串getManager(){
退货经理;
}
公共无效集合管理器(字符串管理器){
this.manager=经理;
}
公共字符串getLocation(){
返回位置;
}
公共void集合位置(字符串位置){
这个位置=位置;
}
}
@实体
公车{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
私有字符串名称;
私有字符串颜色;
私人室内陈列室;
公共汽车(字符串名称、字符串颜色){
这个.setName(name);
这个.setColor(color);
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
public int getShowroomId(){
返回展厅ID;
}
公共无效设置showroomId(int showroomId){
this.showroomId=showroomId;
}
}
目前,我让Hibernate在MySQL数据库中创建表。我已经检查过了,数据库之间的关系确实存在于Car表中
有人能告诉我为什么这不起作用吗
我猜这是因为展厅没有Id,因为这是MySQL自动生成的,所以汽车无法保存?是吗?你这里有几个问题。 在汽车中,您有一个字段,该字段应该是对展厅的FK引用。但是,这是一个本机int。这意味着它的值为零。 如果您的汽车对象应该引用您的展厅,那么您必须添加一个带有@ManyToOne的引用
@ManyToOne
@JoinColumn(name="showroomId")
private Showroom showroom;
然后,您在展厅中的字段将更改为
@OneToMany(mappedBy = "showroom")
@Cascade(value = { org.hibernate.annotations.CascadeType.ALL } )
private List<Car> cars = null;
为了成功地从DB加载实体,我还必须为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;
}
}
因此,如果我先保存展厅(没有汽车),请确保我获取插入的Id并将其设置为展厅上的Id,将汽车添加到展厅并再次保存(更新),这将插入具有展厅Id的汽车?请参阅本教程:谢谢,我将查看。我尝试了您的建议,执行后返回错误“标记为mappedBy的关联不能定义数据库映射,如@JoinTable或@JoinColumn”Oops.Yep。JoinColumn注释属于展厅字段定义中的Car对象内部。我还必须在Car对象内部添加一个默认构造函数,然后才能从DB加载它们。没有默认构造函数(在能见度降低的情况下可以正常工作)我的应用程序崩溃了。我编辑了我的答案以反映这两点。
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();