title: Hibernate PostgreSQL OneToOne Relationship Trigger Child Query First
body: <p>I'm building app to learn Hibernate on PostgreSQL. And I'm currently trying to add variable to database that has OneToOne relationship.</p>
  <p>First, I create two tables in my database with schema below. On <code>person_detail</code> table it has primary key that's also foreign key to <code>person</code> table.</p>
  <p><a href="https://i.stack.imgur.com/lBnl4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lBnl4.png" alt="Database Schema" /></a></p>
  <p>Then I created two classes, <code>Person</code> and <code>PersonDetail</code>. <code>PersonDetail</code> is child of <code>Person</code> that has OneToOne relationship. And I use code below to add <code>person</code> with <code>personDetail</code> as attribute.</p>
  <pre><code>Person person = new Person(
          &quot;Rick&quot;,
          1.7,
          dateFromString(&quot;1969-4-2&quot;),
          new Date()
  );
  PersonDetail personDetail =
          new PersonDetail(&quot;myemail@email.com&quot;, &quot;Marley&quot;);
  person.setPersonDetail(personDetail);
  
  session.beginTransaction();
  
  session.save(person);
  session.save(personDetail);
  
  session.getTransaction().commit();
  
  System.out.println(person.toString());
  </code></pre>
  <p>But the problem with code above is that Hibernate execute child query first instead of parent query.</p>
  <blockquote>
  <p>Hibernate: insert into person_detail (address, email) values (?, ?)</p>
  </blockquote>
  <p>And since <code>person</code> still empty, we cannot insert any row to <code>person_detail</code> because it violates foreign key constraint.</p>
  <p>Is there a way to solve this?
  Thanks!</p>
  <p>In case some one want to check how I annotate those two classes, I put the code below.</p>
  <pre><code>@Entity
  @Table(name=&quot;person&quot;)
  @Data
  public class Person {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name=&quot;id&quot;)
      private int id;
  
      @Column(name=&quot;name&quot;)
      private String name;
  
      @Column(name=&quot;height&quot;)
      private double height;
  
      @Column(name=&quot;birth_date&quot;)
      private Date dateBirth;
  
      @Column(name=&quot;last_seen&quot;)
      private Date lastSeen;
  
      @OneToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;id&quot;)
      private PersonDetail personDetail;
  
      public Person() {}
  
      public Person(String name, double height, Date dateBirth, Date lastSeen){
          this.name = name;
          this.height = height;
          this.dateBirth = dateBirth;
          this.lastSeen = lastSeen;
      }
  }
  
  
  @Data
  @Entity
  @Table(name=&quot;person_detail&quot;)
  public class PersonDetail {
      @Id
      @Column(name=&quot;id&quot;)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
  
      @Column(name=&quot;email&quot;)
      private String email;
  
      @Column(name=&quot;address&quot;)
      private String address;
  
      public PersonDetail(){}
  
      public PersonDetail(String email, String address){
          this.email = email;
          this.address = address;
      }
  }
  </code></pre>