使用Hibernate 5.1.10。我有一个包含部门和员工列表的公司,并且我想将员工放在不同的部门中。这是我的实体:
@Entity
public class Company implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="company_id")
private long id;
private String name;
@OneToMany(mappedBy="company", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Employee> employees;
@OneToMany(mappedBy="company", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Department> departments;
...
}
@Entity
@IdClass(EmployeeId.class)
public class Employee implements Serializable {
public static class EmployeeId implements Serializable {
private long id;
private Company company;
@Override
public int hashCode() {...}
@Override
public boolean equals(Object obj) {...}
}
@Id
@Column(name="employee_id")
private long id;
@Id
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="company_id", updatable=false, insertable=false)
private Company company;
private String name;
...
}
@Entity
@IdClass(DepartmendId.class)
public class Department implements Serializable {
public static class DepartmendId implements Serializable {
private long id;
private Company company;
@Override
public int hashCode() {...}
@Override
public boolean equals(Object obj) {...}
}
@Id
@Column(name="department_id")
private long id;
@Id
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="company_id", updatable=false, insertable=false)
private Company company;
private String name;
@OneToMany(mappedBy="department", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<DepartmentEmployee> employees;
...
}
@Entity
@IdClass(DepartmentEmployeeId.class)
public class DepartmentEmployee implements Serializable {
public static class DepartmentEmployeeId implements Serializable {
private Department department;
private Employee employee;
@Override
public int hashCode() {...}
@Override
public boolean equals(Object obj) {...}
}
@Id
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="department_id", updatable=false, insertable=false),
@JoinColumn(name="company_id", updatable=false, insertable=false)
})
private Department department;
@Id
@OneToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="employee_id", updatable=false, insertable=false),
@JoinColumn(name="company_id", updatable=false, insertable=false)
})
private Employee employee;
private int rating;
...
}
这将创建以下表格:
create table Company (
company_id bigint generated by default as identity,
name varchar(255),
primary key (company_id)
)
create table Department (
department_id bigint not null,
name varchar(255),
company_id bigint not null,
primary key (company_id, department_id)
)
create table Employee (
employee_id bigint not null,
name varchar(255),
company_id bigint not null,
primary key (company_id, employee_id)
)
create table DepartmentEmployee (
rating integer not null,
employee_id bigint not null,
company_id bigint not null,
department_id bigint not null,
primary key (department_id, company_id, employee_id)
)
到目前为止,一切都很好。我可以创建一个拥有部门和员工的公司,并且可以毫无问题地坚持下去。但是,一旦我尝试将一些雇员添加到部门中,就会出现以下错误:
26-03-2019 09:30:29 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (main) SQL Error: 90008, SQLState: 90008
26-03-2019 09:30:29 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (main) Invalid value "5" for parameter "parameterIndex" [90008-196]
到目前为止,我所能收集到的是DepartmentEmployeeId
类中有些混乱,在该类中,hibernate认为有4个ID列,而不是3列。但是我没有发现任何好处解决方案。我如何让Hibernate理解company_id
在IdClass中只能使用一次?对我来说,通过每个表使用单个ID重写数据模型对我来说不是一个选择。