我有一个多对多的关系,如下所示。
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EMP_ID")
private int id;
private String firstName;
private String lastName;
@JoinTable(name = "employee_project_mapping", joinColumns = @JoinColumn(name = "EMPLOYEE_ID", referencedColumnName = "EMP_ID"), inverseJoinColumns = @JoinColumn(name = "PROJECT_ID", referencedColumnName = "PJT_ID"))
@JsonManagedReference
@ManyToMany(cascade = CascadeType.ALL)
Set<Project> projects = new HashSet<Project>();
.....
.....
}
@Entity
@Table(name = "projects")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PJT_ID")
private int projectId;
private String projectName;
private int teamSize;
@ManyToMany(mappedBy = "projects")
@JsonBackReference
private Set<Employee> emps = new HashSet<Employee>();
.....
.....
}
@Entity
@Table(name = "employee_project_mapping")
public class EmployeeProjectMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EMP_PJT_ID")
private Integer empPjtId;
@Column(name = "PROJECT_ID")
private Integer projectId;
@Column(name = "EMPLOYEE_ID")
private Integer emploeeId;
.....
.....
}
但是当我尝试插入带有一组项目的员工对象时,它无法为列 EMP_PJT_ID 创建自动生成的 ID(这是映射表记录的 ID)。我不能使用 jpa 为映射表添加自动生成的 id 吗?
错误跟踪
Hibernate: insert into employees (emp_id, first_name, last_name) values (null, ?, ?)
Hibernate: insert into employee_project_mapping (employee_id, project_id) values (?, ?)
2021-04-22 23:34:25.973 ERROR 24126 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : NULL not allowed for column "EMP_PJT_ID"; SQL statement:
insert into employee_project_mapping (employee_id, project_id) values (?, ?) [23502-200]
2021-04-22 23:34:25.975 ERROR 24126 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "EMP_PJT_ID"; SQL statement:
insert into employee_project_mapping (employee_id, project_id) values (?, ?) [23502-200]
答案 0 :(得分:0)
多对多的映射应该是:
@Entity
@Table(name = "employees")
public class Employee {
...
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
Set<EmployeeProjectMapping> projects = new HashSet<EmployeeProjectMapping>();
...
// Utility method to update both sides of the association
public void addProject(Project project) {
EmployeeProjectMapping empProject = new PersEmployeeProjectMappingonAddress( this, project );
projects.add( empProject );
project.getEmployees().add( empProject );
}
}
@Entity
@Table(name = "projects")
public class Project {
...
@OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<EmployeeProjectMapping> emps = new HashSet<Employee>();
...
}
@Entity
@Table(name = "employee_project_mapping")
public class EmployeeProjectMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EMP_PJT_ID")
private Integer empPjtId;
@Id
@ManyToOne
@JoinColumn(name = "PROJECT_ID")
private Project project;
@Id
@ManyToOne
@JoinColumn(name = "EMPLOYEE_ID")
private Employee employee;
.....
.....
}
请务必检查 the example on the Hibernate ORM documentation 以了解所有详细信息。