title: JPA + Hibernate - How to get FK of child entity without fetching that entity?
body: <p>A possible answer to my question is located here:
  <a href="https://stackoverflow.com/questions/3941797/how-can-i-retrieve-the-foreign-key-from-a-jpa-manytoone-mapping-without-hitting">How can I retrieve the foreign key from a JPA ManyToOne mapping without hitting the target table?</a></p>
  
  <p>However, the preferable solution (property access) does not work in my case (I got missing column exception - why?)</p>
  
  <p>The model looks like this: Entities <code>Parent</code> and <code>Child</code>. Table <code>parent</code> has column <code>child_id</code> which is <code>PK</code> of <code>child</code> table so it is typical <code>@ManyToOne</code> relation.</p>
  
  <p>Now the point is, if I fetch <code>Parent</code> entities, I need to have access to <code>FK</code> value (aka. <code>PK</code> of <code>Child</code> entity) without fetching <code>Child</code> entities. How can I do that?</p>
  
  <p>I use <code>Annotations</code> and my mapping looks as follows:</p>
  
  <pre><code>@Entity
  @Table(name = "parent")
  public class Parent extends AbstractEntity {
  
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name = "patent_id", nullable = true)
  private Child child;
  
  @Column(name="child_id",insertable=false,updatable=false)
  private Integer childId;
  
  public Child getChild() {
      return patent;
  }
  
  public void setChild(Child child) {
      this.child = child;
  }
  
  
  public Integer getChildId(){
      return childId;
  }
  }
  </code></pre>
  
  <p>And what I want to do is call <code>parent.getChild().getId()</code> without extra fetches of <code>Child</code> entity from DB.</p>
  
  <p>According to the answer I have mentioned above, If I moved annotations from field to getter (in <code>Parent</code> entity am I right?), requested behavior would be out of the box. However, when I move annotations to getter, I get a validation exception that <code>child</code> column is missing (curious, why <code>child</code> not <code>child_id</code> as declared?)</p>
  
  <p><strong>PS:</strong> Shown workaround to declare a <code>FK</code> column as separate field works fine, but I don't think that this is the way it should be done.</p>