多对多映射显示错误结果

时间:2019-04-16 17:14:56

标签: hibernate spring-mvc jpa hibernate-mapping

我创建了两个实体Employee和Projects,Employee和Projects的代码如下所示

Employee.java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long empId;
@Column(name = "empName")
private String employeeName;
@Column(name = "designation")
private String designation;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "emp_project_detail",joinColumns = {@JoinColumn(name = "e_id")},inverseJoinColumns = {@JoinColumn(name = "p_id")})
private List<Project> projectList;
//getters and setters

Project.java

 @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "projectid")
private  Long projectId;
@Column(name = "projectname")
private  String projectName;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "emp_project_detail",joinColumns ={@JoinColumn(name = "p_id")},inverseJoinColumns = {@JoinColumn(name = "e_id")})
List<Employee> employeeList;

我已经创建了一个InsertController来为Employee分配多个项目,如下所示

@RequestMapping(name = "/assignedProject")
private String project_empDetails(Model model){
    List<Employee> employeeList= new ArrayList<Employee>();
    List<Project> projects = new ArrayList<Project>();
   Session session =  sessionFactory.getCurrentSession();
   session.beginTransaction();
    Project project_1 =new Project("Banking Projects");
    Project project_2 =new Project("IT Projects");
    Employee employee_1 = new Employee("Arul","developer");
    Employee employee_2 = new Employee("Ankita","developer");
 employee_1.setProjectList(projects);
 employee_1.getProjectList().add(project_1);
 employee_1.getProjectList().add(project_2);
 session.save(employee_1);
    session.getTransaction().commit();
    session.close();
    model.addAttribute("msg","Assigned Project Sucessfully");

    return "projectList";

执行此操作后,这些值将被存储在表中“ 3次”,控制台还将显示这些值被存储在3次中。

 Hibernate: insert into employee (designation, emp_detailId, empName) values (?, ?, ?)
Hibernate: insert into project (projectname) values (?)
Hibernate: insert into project (projectname) values (?)
Hibernate: insert into emp_project_detail (e_id, p_id) values (?, ?)
Hibernate: insert into emp_project_detail (e_id, p_id) values (?, ?)
Hibernate: insert into employee (designation, emp_detailId, empName) values (?, ?, ?)
Hibernate: insert into project (projectname) values (?)
Hibernate: insert into project (projectname) values (?)
Hibernate: insert into emp_project_detail (e_id, p_id) values (?, ?)
Hibernate: insert into emp_project_detail (e_id, p_id) values (?, ?)
Hibernate: insert into employee (designation, emp_detailId, empName) values (?, ?, ?)
Hibernate: insert into project (projectname) values (?)
Hibernate: insert into project (projectname) values (?)
Hibernate: insert into emp_project_detail (e_id, p_id) values (?, ?)
Hibernate: insert into emp_project_detail (e_id, p_id) values (?, ?)

下表的快照如下

enter image description here enter image description here

enter image description here

代码有什么问题?

0 个答案:

没有答案