title: Hibernate generating SQL queries when accessing associated entity's id
body: <p>I have Hibernate Entities that look something like this (getters and setters left out):</p>
  
  <pre><code>@Entity
  public class EntityA {
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "parent_id")
      private EntityB parent;
  }
  
  @Entity
  public class EntityB extends SuperEntity {
      @OneToMany(mappedBy = "parent")
      @Fetch(FetchMode.SUBSELECT)
      @JoinColumn(name = "parent_id")
      private Set&lt;EntityA&gt; children;
  }
  
  @MappedSuperclass
  public class SuperEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private long itemId;
  }
  </code></pre>
  
  <p>When I query for EntityA it loads fine, with the parent association being replaced by a Hibernate proxy (as it is Lazy). If I want access to the parent's id I perform the following call:</p>
  
  <pre><code>EntityA entityA = queryForEntityA();
  long parentId = entityA.getParent().getItemId();
  </code></pre>
  
  <p>As I understand that call should NOT make a roundtrip to the database, as the Id is stored in the EntityA table, and the proxy should only return that value. However, in my case this generates a SQL statement which fetches EntityB and only then returns the Id.</p>
  
  <p>How can I investigate the problem? What are some likely causes of this incorrect behaviour?</p>