2016-11-24 11 views
0

私はHibernateを使用してMySQL DB内の2つのテーブルに結合カラムを作成しようとしていますが、動作させることができません。私はこれに本当に新しいので、おそらく私はそれをより早期に調べることから正しく理解しませんでした。Hibernate join columns

私の学校のクイズプロジェクトを作成しています。教師はタスクを作成することができます。私はER diagramを作成しました。私はそれについて変えるべきことがあれば、私はすべての提案に開放されています。

ちょっと面倒だったので、私はちょうど新しいプロジェクトを作成しました(ただし、アイデアは同じです)。コードに紛失しないほうが簡単です。私はBox Box(プロジェクトの教師)とStuff(プロジェクトのTasks)に参加しようとしています。簡単に言えば、アノテーションにいくつかのバリエーションを試しましたが、どちらもうまくいきませんでした。その後

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; 
     } 
     } 

、私はちょうどこのように、DBに箱の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

エンティティにマッピング注釈を配置する場所が一貫している必要があります。あなたはゲッターの上にそれらを置くか、それらをすべてフィールドに置きます。

ゲッターのIdアノテーションをフィールドに追加しますが、ManyToOneアノテーションはフィールド上に置かれるため、ManyToOneはHibernateによって無視されます。

マッピングがあまり意味を持たないことに注意してください。物を一意に識別するために使用される列は、ボックスの参照にも使用できません。また、BoxプロパティにColumnとJoinColumnを同時にアノテーションすることはできません。それはJoinColumnであり、Columnではありません。

+0

ああ、うわー。私はまた、スタフクラスに参加するためのIDを交換しました。さて、助けをありがとう、それは今働く。 –