title: Database view hibernate mapping
body: <p>I am using a Hibernate mapping of my database view (view1), and it contains another view (view2). It works, but I get many requests to the database and I want to optimize this. What is the problem?</p>
  
  <pre><code>@Entity
  @Table(name = "view1")
  public class View1 {
  
      @Id
      @Column(name = "id")
      private Long id;
      @JoinColumn(name = "id", referencedColumnName = "operation", insertable = false, updatable = false)
      @ManyToOne(fetch = FetchType.LAZY)
      private View2 view2;
  ...
  }
  
  @Entity
  @Table(name = "view2")
  public class View2 {
  
      @Id
      @Column(name = "id")
      private Long id;
      @Basic(optional = false)
      @Column(name = "operation")
      private Long operation;
  ...
  }
  </code></pre>
  
  <p>The next JPQL query generate too much requests.</p>
  
  <pre><code>select e from View1 e left join fetch e.view2
  </code></pre>
  
  <p>Generated native SQL, repeated more than 60 times</p>
  
  <pre><code>select view20_.id as id1_221_0_, view20_.operation as operati16_221_0_
  </code></pre>