我想创建使用休眠MySQL数据库中的2个表中的连接列,但是,我似乎无法得到它的工作。我对此很新,所以也许我之前没有正确理解它。休眠加入列
我正在为我的学校创建一个测验项目。老师能够创建任务。我为此创建了一个ER diagram。如果有什么我应该改变的,我愿意接受所有的建议。
它变得有点乱,所以我刚刚创建了一个新项目(但想法是一样的),只是这样更容易不会迷失在代码中。我试图加入表格Box(项目中的教师)和Stuff(项目中的任务)。只是一个简短的说明,我已经尝试了与注释的一些变化,但它也不起作用。
Box类
@Entity
public class Box {
private int size, box_id;
private String name, shape, colour, material;
public Box(String name, int size, String shape, String colour, String material) {
this.name = name;
this.size = size;
this.shape = shape;
this.colour = colour;
this.material = material;
}
public Box() {
}
@Id
@GenericGenerator(name="id" , strategy="increment")
@GeneratedValue(generator="id")
public int getBox_id() {
return box_id;
}
public void setBox_id(int box_id) {
this.box_id = box_id;
}
@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(nullable = false)
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Column(nullable = false)
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
@Column(nullable = false)
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
@Column(nullable = false)
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
}
东西类
@Entity
public class Stuff {
private int amount, stuff_id;
private String name, type, colour, material;
@ManyToOne
@JoinColumn(name = "stuff_id")
private Box box;
public Stuff(String name, int amount, String type, String colour, String material, Box box) {
this.name = name;
this.amount = amount;
this.type = type;
this.colour = colour;
this.material = material;
this.box = box;
}
public Stuff() {
}
@Id
@GenericGenerator(name="id" , strategy="increment")
@GeneratedValue(generator="id")
public int getStuff_id() {
return stuff_id;
}
public void setStuff_id(int stuff_id) {
this.stuff_id = stuff_id;
}
@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(nullable = false)
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Column(nullable = false)
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(nullable = false)
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
@Column(nullable = false)
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
@Column(nullable = false)
public Box getBox() {
return box;
}
public void setBox(Box box) {
this.box = box;
}
}
在那之后,我只是尽量把箱的3个实例和东西的9个实例到数据库,像这样。
new BoxDao().instertBox(new Box("box1", 10, "cube", "green", "cardboard"));
new StuffDao().instertStuff(new Stuff("stuff1", 5, "toys", "red", "plastic", new BoxDao().getBoxById(1)));
我得到的错误是这样的:
Caused by: org.hibernate.MappingException: Could not determine type for: model.Box, at table: Stuff, for columns: [org.hibernate.mapping.Column(box)]
我欣赏每一个答案,如果对不起它的东西很明显,但我一定是刚刚错过了什么问题。谢谢。
哦,哇。我甚至将ID也换成了Stuff类。那么,感谢您的帮助,它现在起作用。 –