title: Avoid unnecessary join in many-to-many relationship
body: <p>I have two tables in my database, called <code>media_blog</code> and <code>media_category</code>. There is a third table called <code>media_blog_category</code>, which relates blogs to the categories they belong to (a blog can belong to many categories, and a category may contain many blogs). This relationship is mapped in Hibernate/JPA as follows: </p>
  
  <pre><code>@Entity
  @Table(name="media_blog")
  public class Blog {
      @Column(name="name")
      private String name;
  
      @ManyToMany(fetch = FetchType.LAZY, targetEntity=MediaCategory.class)
      @JoinTable(name = "media_blog_category", 
          joinColumns = {@JoinColumn(name = "blog_id")}, 
          inverseJoinColumns = {@JoinColumn(name = "media_category_id")}
      )
      private Set&lt;MediaCategory&gt; categories;
  
      // Here goes the setters and getters for fields name and categories
  }
  
  @Entity
  @Table(name="media_category")
  public class MediaCategory {
      @Column(name="name")
      private String name;
  
      @ManyToMany(fetch = FetchType.LAZY, mappedBy="categories")
      private List&lt;Blog&gt; blogs;
  
      // Here goes the setters and getters for fields name and blogs
  }
  </code></pre>
  
  <p>When I retrieve a blog from the database, using <code>Blog b = Blog.find(id)</code> the following single query is logged into debug console:</p>
  
  <pre><code>select blog0_.id as id11_0_, blog0_.name as name11_0_ 
  from media_blog blog0_ where blog0_.id=?
  </code></pre>
  
  <p>The lazy fetch type set on field <code>categories</code> works as expected, since no category information was queried.</p>
  
  <p>But the following query is performed when doing <code>Set&lt;MediaCategory&gt; blogCategories = b.getCategories()</code>:</p>
  
  <pre><code>select categories0_.blog_id as blog1_11_1_, 
      categories0_.media_category_id as media2_1_, 
      mediacateg1_.id as id2_0_,
      mediacateg1_.name as name2_0_ 
  from media_blog_category categories0_ 
  inner join media_category mediacateg1_ 
     on categories0_.media_category_id=mediacateg1_.id 
  where categories0_.blog_id=?
  </code></pre>
  
  <p>The problem is that I'm interested in the category IDs only (field <code>media_category_id</code> in table <code>media_blog_category</code>), but I noticed the query does a join with the <code>media_category</code> table and retrieves all fields of each category (in this case, the field <code>name</code>, but there will be more in the future) in order to populate the MediaCategory objects. There is a performance penalty, since it is doing joins to get information I'm not going to use.</p>
  
  <p>Is there a way to avoid Hibernate to do this join, so it populates the variable <code>blogCategories</code> above with MediaCategory proxies (where only the <code>id</code> field is available - that's all I need), and delay the retrieval of the rest of category fields to the moment when I invoke the "getName()" (or another column getter) of a MediaCategory proxy object?</p>
  
  <p>What I'm trying to do is similar to what is done in this post:
  <a href="http://256stuff.com/gray/docs/misc/hibernate_lazy_field_access_annotations.shtml" rel="nofollow">http://256stuff.com/gray/docs/misc/hibernate_lazy_field_access_annotations.shtml</a></p>
  
  <p>But for many-to-many relationships.
  I've tried the approach shown in that post, so annotated the <code>id</code> field in class <code>MediaCategory</code> with <code>@AccessType("property")</code>, but it didn't work. It seems it is not suited for many to many relationships, but for one-to-one relationships only.</p>
  
  <p>Thanks!</p>