title: Update using one to one relationship hibernate
body: <p>I have one parent object as Employee and child object as Address. I just need to update the both objects using Employee object. But when updating using employee object i just getting emp_id should not be null. Here is my table and entity</p>
  
  <pre><code>CREATE TABLE `employee` (
  `employee_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `employee_name` varchar(30) NOT NULL,
  `employee_desg` varchar(30) NOT NULL,
  `salary` varchar(30) NOT NULL,
  `employee_reference_id` varchar(10) NOT NULL,
  PRIMARY KEY (`employee_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>Address table </p>
  
  <pre><code>CREATE TABLE `address` (
  `address_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `emp_id` bigint(20) NOT NULL,
  `address` varchar(255) NOT NULL,
  PRIMARY KEY (`address_id`),
  KEY `employee_address` (`emp_id`),
  CONSTRAINT `employee_address` FOREIGN KEY (`emp_id`) REFERENCES `employee`   (`employee_id`) ON DELETE CASCADE ON UPDATE CASCADE
  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>Employee Object</p>
  
  <pre><code>import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Employee")
  public class Employee {
  
  @Id
  @GeneratedValue
  @Column(name = "EMPLOYEE_ID")
  private int id;
  
  @Column(name = "EMPLOYEE_NAME")
  private String employeeName;
  
  @Column(name = "EMPLOYEE_DESG")
  private String employeeDesg;
  
  @Column(name = "SALARY")
  private String salary;
  
  @Column(name = "EMPLOYEE_REFERENCE_ID")
  private String employeeReferenceId;
  
  public String getEmployeeReferenceId() {
      return employeeReferenceId;
  }
  
  public void setEmployeeReferenceId(String employeeReferenceId) {
      this.employeeReferenceId = employeeReferenceId;
  }
  
  @OneToOne(mappedBy="employee", cascade = CascadeType.ALL)
  private Address address;
  
  
  public String getEmployeeName() {
      return employeeName;
  }
  
  public void setEmployeeName(String employeeName) {
      this.employeeName = employeeName;
  }
  
  public String getEmployeeDesg() {
      return employeeDesg;
  }
  
  public void setEmployeeDesg(String employeeDesg) {
      this.employeeDesg = employeeDesg;
  }
  
  public String getSalary() {
      return salary;
  }
  
  public void setSalary(String salary) {
      this.salary = salary;
  }
  
  public Address getAddress() {
      return address;
  }
  
  public void setAddress(Address address) {
      this.address = address;
  }
  
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  }
  </code></pre>
  
  <p>Address Object</p>
  
  <pre><code>import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  
  
  
  @Entity
  @Table(name = "ADDRESS")
  public class Address {
  
  @Id
  @Column(name="ADDRESS_ID")
  @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;
  
  @Column(name = "ADDRESS")
  private String address;
  @OneToOne(fetch=FetchType.EAGER , cascade=CascadeType.ALL, orphanRemoval=true)
  @JoinColumn(name="emp_id",referencedColumnName="employee_id")
  private Employee employee;
  
  public String getAddress() {
      return address;
  }
  
  public void setAddress(String address) {
      this.address = address;
  }
  
  public Employee getEmployee() {
      return employee;
  }
  
  public void setEmployee(Employee employee) {
      this.employee = employee;
  }
  
  public Address() {
  
  }
  
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  }
  </code></pre>
  
  <p>My code is</p>
  
  <pre><code>public class StudentUtil1To1 {
  public static void main(String args[]){
      SessionFactory factory=null;
      Configuration configuration=null;
      ServiceRegistry registry=null;
      Session session=null;
      try{
          configuration= new Configuration();
          configuration.configure();
          registry=new StandardServiceRegistryBuilder().configure().applySettings(configuration.getProperties()).build();
          factory=configuration.configure("hibernate.cfg.xml").buildSessionFactory(registry);
          session= factory.openSession();
          session.beginTransaction();
  
  
              Employee emp=new Employee();
              emp.setId(1);
              emp.setEmployeeReferenceId("CP001");
              emp.setEmployeeName("Muthu");
              emp.setEmployeeDesg("Developer");
              emp.setSalary("15000");
  
              Address address=new Address();
              address.setAddress("3, Civil aerodrome, CBE");
  
  
              emp.setAddress(address);
              address.setEmployee(emp);
              session.update(emp);
  
  
  
          System.out.println("Successfuly Saved");
          session.getTransaction().commit();
  
      }catch(Exception e){
          e.printStackTrace();
      }finally{
          if(session!=null){
              session.close();
          }
          if(factory!=null){
              factory.close();
          }
      }
  }
  }
  </code></pre>
  
  <p>And the error is</p>
  
  <pre><code>09:40:48.815 [http-nio-8081-exec-7] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1048, SQLState: 23000
  09:40:48.816 [http-nio-8081-exec-7] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Column 'emp_id' cannot be null
  </code></pre>
  
  <p>What i need to for update. Correct my mistake. </p>