title: Eager fetching in a Spring Specification
body: <p>We have a Media object:</p>
  
  <pre><code>public class Media implements Serializable {
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(insertable = false, updatable = false)
      private Long id;
      // other attributes
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "channelId", referencedColumnName = "id")
      private Channel channel;
      // getters, setters, hashCode, equals, etc.
  </code></pre>
  
  <p>}</p>
  
  <p>The eager fetch of the channel parent works in regular repository methods, but not when using a Specification.</p>
  
  <p>Here's the Specification:</p>
  
  <pre><code>public class MediaSpecs {
  
  public static Specification&lt;Media&gt; search(final Long partnerId, final Integer width, final Integer height,
          final String channelType) {
  
      return new Specification&lt;Media&gt;() {
  
          @Override
          public Predicate toPredicate(Root&lt;Media&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
              Predicate restrictions = cb.equal(root.get("archived"), false);
              // other restrictions are and-ed together
              if (channelType != null) {
                      Join&lt;Media, ChannelType&gt; join = root.join("channel").join("orgChannelType").join("type");
                  restrictions = cb.and(cb.equal(join.get("type"), channelType));
              }
              return restrictions;
          }
      };
  }
  </code></pre>
  
  <p>The "search" spec works correctly when channelType is specified, so the join is working. How do I specify that the joins should be eagerly fetched?</p>
  
  <p>I tried adding</p>
  
  <pre><code>Fetch&lt;Media, ChannelType&gt; fetch = root.fetch("channel").fetch("orgChannelType").fetch("type");
  </code></pre>
  
  <p>Then Hibernate throws an exception:</p>
  
  <p>org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=generatedAlias4 ...</p>
  
  <p>How do I add the associations to the select list?</p>
  
  <p>Thanks.</p>