2016-11-24 95 views
0

我想创建使用休眠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)] 

我欣赏每一个答案,如果对不起它的东西很明显,但我一定是刚刚错过了什么问题。谢谢。

回答

1

您必须在将映射注释放置在实体中的位置上保持一致。要么你把它们全部放在吸气剂上,要么全部放在田间。

由于您将get注释放在了get注释中,但是该字段上的ManyToOne注释会忽略Hibernate的ManyToOne。

请注意,映射没有多大意义:用于唯一标识内容的列不能用于引用框。 box属性不能用Column和JoinColumn同时注释。它是一个JoinColumn,而不是一个Column。

+0

哦,哇。我甚至将ID也换成了Stuff类。那么,感谢您的帮助,它现在起作用。 –