title: Return ResultSet with intact joins in Hibernate / JPA
body: <p>I have 2 entities: EntityA and EntityB. They are related with a One To Many relation.</p>
  
  <pre><code>public class EntityA {
  
      @Identifier
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="ID", updatable = false, nullable = false)
      private long id;
  
      @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
      @JoinColumn(name="ENTITY_A_ID", referencedColumnName="ID", nullable=true)
      private List&lt;EntityB&gt; entityBs;
  
     /* GETTERS SETTERS ... */
  }
  </code></pre>
  
  <pre><code>public class EntityB {
  
      @Identifier
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="ID", updatable = false, nullable = false)
      private long id;
  
  
      @Column(name="SOME_PROPERTY")
      private String someProperty;
  
      @ManyToOne
      @JoinColumn(name="ENTITY_A_ID")
      private EntityA entityA;
      /* GETTERS SETTERS ... */
  
  }
  </code></pre>
  
  <p>I have a query that joins EntityA with a LEFT JOIN to Entity B. And a 'ON' clause. 
  In normal SQL lingo this would be:</p>
  
  <pre><code>select * from EntityA eA left join EntityB eB
      on (eA.ID = eB.ENTITY_A_ID and eB.SOME_PROPERTY = "blabla" )
      where ...
  </code></pre>
  
  <p>So I'm having much needed information from my joined resultset. I only want records joined if they match certain properties. I need EntityA, allways, and an attached EntityB if EntityB matched the join clause.</p>
  
  <p>The project is set up with Hibernate / JPA. I can't figure out how to retreive the information needed. At this moment I have:</p>
  
  <pre><code>public class EntityADAO {
  
      public List&lt;EntityA&gt; findMethod() {
  
          CriteriaBuilder builder = entityManager.getCriteriaBuilder();
  
          CriteriaQuery&lt;EntityA&gt; query = builder.createQuery(EntityA.class);
  
          Root&lt;EntityA&gt; entityARoot = query.from(EntityA.class);
          Join&lt;EntityA, EntityB&gt; entityBJoin = entityARoot.join("entityB", JoinType.INNER);
          entityBJoin.on(new Predicate [] {builder.equal(entityBJoin.get("someProperty"), "fixed_val_for_now"});
  
         /* where clause left out for readability */
  
          TypedQuery&lt;EntityA&gt; q = entityManager.createQuery(query);
  
          return q.getResultList();
  
          }
  
      } 
  </code></pre>
  
  <p>So here I am.. Stuck with my List of EntityAs. whenever I call getEntityBs() on a EntityA, I'm getting all of them.. And this makes sense.. But How can I retrieve the joined set? </p>
  
  <p>I'm stuck with JPA and Hibernate, as this choice is not made by me.</p>
  
  <p>Thanks in advance!</p>