2016-08-19 72 views
0

我學習Hibernate和卡位與下面的問題試圖瞭解如何@JoinTable和@JoinColumn工作

有兩個表

CREATE TABLE department (
department_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
caption varchar(255) DEFAULT NULL) ENGINE=InnoDB; 
CREATE TABLE employee (
employee_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
fio varchar(255) DEFAULT NULL, 
fk_department_id int(11) NOT NULL, 
FOREIGN KEY (fk_department_id) REFERENCES department (department_id) 
) ENGINE=InnoDB ; 

和兩班(第一類註釋掉的代碼貌似工作液)

@Entity 
@Table(name = "department") 
public class Department { 
.... 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "employee", joinColumns = { 
      @JoinColumn(name = "fk_department_id", referencedColumnName = "department_id") }) 
    /* 
    * @OneToMany(fetch = FetchType.LAZY, mappedBy = "department", cascade = 
    * CascadeType.ALL) 
    */ 
    public Set<Employee> getEmployies() { 
     return employees; 
    } 

@Entity 
@Table(name = "employee") 
public class Employee { 
...... 
    @ManyToOne 
    @JoinColumn(name = "fk_department_id") 
    public Department getDepartment() { 
     return department; 
    } 

這導致到

INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 
Exception in thread "main" org.hibernate.MappingException: Foreign key (FK3cspe1b06hmsik5l8y1i11xmd:employee [employies_employee_id])) must have same number of columns as the referenced primary key (employee [fk_department_id,employies_employee_id]) 
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:148) 
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:130) 

請幫我明白爲什麼這不起作用

回答

0

下應該只是罰款。你會注意到我沒有指定任何連接列關係,因爲我允許Hibernate爲我自動生成這些關係。

@Entity 
public class Department { 
    @OneToMany 
    @JoinTable(name = "department_employees") 
    private List<Employee> employees; 
} 

@Entity 
public class Employee { 
    @ManyToOne 
    private Department department; 
} 

但讓我們假設你想明確的加入列。

@Entity 
public class Department { 
    @Id 
    @Column(name = "department_id") 
    private Integer id; 
    @OneToMany 
    @JoinTable(
    name = "department_employees", 
    joinColumns = @JoinColumn(name = "department_id"), 
    inverseJoinColumns = @JoinColumn(name = "employee_id")) 
    private List<Employee> employees; 
} 

@Entity 
public class Employee { 
    @Id 
    @Column(name = "employee_id") 
    private Integer id; 
    @ManyToOne 
    @JoinTable(
    name = "department_employees", 
    joinColumns = @JoinColumn(name = "department_id", insertable = false, updatable = false), 
    inverseJoinColumns = @JoinColumn(name = "employee_id", insertable = false, updatable = false)) 
    private Department department; 
} 

的關鍵點採取遠離這一切是:

  • 的連接表的名稱指定保持DepartmentEmployee實體之間的關係中間表。它不應該引用Employee表作爲您的代碼說明。
  • joinColumns屬性表示包含實體的主鍵屬性,在這種情況下是Department,因此我使用了department_id
  • inverseColumns屬性代表關聯實體的主要關鍵屬性,在這種情況下是Employee,因此我使用了employee_id

更新:

如果你想消除@JoinTable和僅僅維持DepartmentEmployee之間的關係,你會改變你的映射如下:

@Entity 
public class Department { 
    @OneToMany(mappedBy = "department") 
    private List<Employee> employees; 
} 

@Entity 
public class Employee { 
    @ManyToOne 
    private Department department; 
} 

希望幫助。

+0

謝謝Naros的回答, 在我的情況下,我沒有中間表,我只有2個表 –

+0

這聽起來像你不想加Join表。如果您願意的話,您可以輕鬆映射OneToMany和ManyToOne而無需JoinTable。通過發佈更新以包含沒有JoinTable的關係映射。 – Naros

+0

我是否正確理解JoinTable與JoinColumn的工作原理,當我有3個表? –