There is a bidirectional one-to-many relationship between Department and Employee.
@Setter
@Getter
@Entity
@Table(name = "t_department")
public class Department {
@Id
private String id;
private String name;
@OneToMany(mappedBy = "department",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private List<Employee> employees;
}
@Setter
@Getter
@Entity
@Table(name = "t_employee")
public class Employee {
@Id
private String id;
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "dept_id")
private Department department;
}
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, String> {
}
In the database, I have those records.
t_department:
+----+------------+
| id | name |
+----+------------+
| 2 | accounting |
| 3 | logistics |
+----+------------+
t_employee:
+----+------+---------+
| id | name | dept_id |
+----+------+---------+
| 3 | Tom | 2 |
| 4 | Tina | 3 |
+----+------+---------+
When I tried to delete an Employee(id="3"),
@Test
@Transactional
public void should_delete_employee_success_when_delete_employee_given_a_exist_employee_id_in_DB() {
employeeRepository.delete("3");
}
But in console, it only printed 2 select statements without deleting:
Hibernate: select employee0_.id as id1_2_0_, employee0_.dept_id as dept_id3_2_0_, employee0_.name as name2_2_0_, department1_.id as id1_1_1_, department1_.name as name2_1_1_ from t_employee employee0_ left outer join t_department department1_ on employee0_.dept_id=department1_.id where employee0_.id=?
Hibernate: select employees0_.dept_id as dept_id3_2_0_, employees0_.id as id1_2_0_, employees0_.id as id1_2_1_, employees0_.dept_id as dept_id3_2_1_, employees0_.name as name2_2_1_ from t_employee employees0_ where employees0_.dept_id=?
And I went to see the database, Nothing has been done.
How does spring-data-jpa works? I'm confused for several days.
Thank you for your answers in advance.