The main problem is that you use inputs that contain too much information for what you want to do. And that makes everything confusing.
All you want to do is setting an existing program on an existing student. The only inputs you need for this task are the student ID and the program ID. And if you had only those inputs, the solution would become obvious:
@Transactional
public void setProgramOnStudent(Long studentId, Long programId) {
Student student = (Student) session.get(Student.class, studentId);
Program program = (Program) session.load(Program.class, programId);
student.setProgram(program);
}
Note that I used load()
and not get()
to load the program, because you don't ven need th state of the program, only a reference to the existing program. It would also work with get()
, but it would execute an additional, useless seelct query to load the state of the program.