2016-08-19 36 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个表? –