title: Add data to a related table Spring Hibernate
body: <p>I have two entites:</p>
  <p><code>Employee:</code></p>
  <pre><code> @Entity
  @Table(name=&quot;employee&quot;)
  public class Employee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = &quot;employeeId&quot;)
      private int id;
  
      @Column(name = &quot;first_name&quot;)
      private String firstName;
  
      @Column(name = &quot;last_name&quot;)
      private String lastName;
  
      @Column(name = &quot;email&quot;)
      private String email;
  
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;employeeId&quot;)
      private EmployeeDetail employeeDetail;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public EmployeeDetail getEmployeeDetail() {
          return employeeDetail;
      }
  
      public void setEmployeeDetail(EmployeeDetail employeeDetail) {
          this.employeeDetail = employeeDetail;
      }
  }
  </code></pre>
  <p><code>EmployeeDetail:</code></p>
  <pre><code>    @Entity
  @Table(name = &quot;employee_detail&quot;)
  public class EmployeeDetail {
  
      @Id
      @GeneratedValue(strategy= GenerationType.AUTO)
      @Column(name=&quot;employee_detail_id&quot;)
      private int id;
  
      @Column(name=&quot;work_experience&quot;)
      private int workExperience;
  
      @Column(name=&quot;hobby&quot;)
      private String hobby;
  
      @Column(name=&quot;nationality&quot;)
      private String nationality;
  
      @OneToOne(mappedBy = &quot;employeeDetail&quot;)
      private Employee employee;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public int getWorkExperience() {
          return workExperience;
      }
  
      public void setWorkExperience(int workExperience) {
          this.workExperience = workExperience;
      }
  
      public String getHobby() {
          return hobby;
      }
  
      public void setHobby(String hobby) {
          this.hobby = hobby;
      }
  
      public String getNationality() {
          return nationality;
      }
  
      public void setNationality(String nationality) {
          this.nationality = nationality;
      }
  
      public Employee getEmployee() {
          return employee;
      }
  
      public void setEmployee(Employee employee) {
          this.employee = employee;
      }
  }
  </code></pre>
  <p>On this way I store data to <code>Employee</code> table:</p>
  <p><code>EmployeeController:</code></p>
  <pre><code>   @Controller
  @RequestMapping(&quot;/employee&quot;)
  public class EmployeeController {
      @Autowired
      private EmployeeService employeeService;
  
      @GetMapping(&quot;/list&quot;)
      public String listEmployee(Model theModel) {
  
          // get customers from the service
          List&lt;Employee&gt; theEmployee = employeeService.listEmployee();
  
          // add the customers to the model
          theModel.addAttribute(&quot;employees&quot;, theEmployee);
  
          return &quot;list-employee&quot;;
      }
  
      @GetMapping(&quot;/showFormForAddNewEmployee&quot;)
      public String showFormForAddNewEmployee(Model theModel) {
  
          // create model attribute to bind form data
          Employee theEmployee = new Employee();
  
          theModel.addAttribute(&quot;employee&quot;, theEmployee);
  
          return &quot;addNewEmployeeForm&quot;;
      }
  
      @PostMapping(&quot;/saveEmployee&quot;)
      public String saveEmployee(@ModelAttribute(&quot;customer&quot;) Employee theEmployee) {
  
          // save the customer using our service
          employeeService.saveEmployee(theEmployee);
  
          return &quot;redirect:/employee/list&quot;;
      }
  }
  </code></pre>
  <p>How to create something like that for related table? Im confused</p>