ManytoMany Mapping показывает неверный результат - PullRequest
0 голосов
/ 16 апреля 2019

Я создал две сущности, а именно 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 для назначения нескольких проектов для Сотрудника, как показано ниже

@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

Что не так в коде?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...