There are two tables in the H2 database: employees and department.
In employees there are fields: ID , DEPARTMENT , FULLNAME , DEPARTMENT_ID .
In department: ID , DEPARTMENT .
Foreign key is provided for department.ID - employees.DEPARTMENT_ID .
I want to make it so that when a new employee is added to the table, the DEPARTMENT_ID field is automatically put down a value equal to the ID field value in the department table for the corresponding DEPARTMENT in both tables.
How can this be implemented?
At the moment I have this request:
insert into employees(fullName, department) values(:fullName, :department) and method:
@Value("${query.insertNewEmployee}") private String insertNewEmployee; @Override public void insertNewEmployee(String fullName, String department) { Map<String, String> map = new HashMap<>(2); map.put("fullName", fullName); map.put("department", department); jdbcTemplate.update(insertNewEmployee, map); }
Select Employees.*,Department.department from Employees left join Department on Employees.DEPARTMENT_ID = Department.ID- Aleksei Chibisov