title: JPA annotation with association table
body: <p>I'm using JPA annotation with Hibernate and I don't really know how to manage an <em>association table</em>.</p>
  <h2>The entity relationship</h2>
  <p>Here is my database schema as entity-relationship (ER) diagram:</p>
  <pre><code>  COUNTRY          COUNTRY_MOVIE        
  |---------|*    *|---------------|      | MOVIE   |
  | COU_ID  |&lt;----&gt;| COUNTRY_ID    |*    *|---------|
  | LABEL   |      | MOVIE_ID      |&lt;----&gt;| MO_ID   |
  |---------|      |---------------|      | LABEL   |
                                          |---------|
  </code></pre>
  <p>The table <code>COUNTRY_MOVIE</code> is an <em>association table</em> between <code>COUNTRY</code> and <code>MOVIE</code> with many-to-many associations (symbolized by the arrow-lines above where many-to-many expressed by <code>*  *</code>) .</p>
  <h2>The entities modeled in code with JPA</h2>
  <p>In my JPA object I didn't create a <code>CountryMovie.java</code> class.</p>
  <h3>Country</h3>
  <p>But I use <code>@JoinTable</code> in <code>Country.java</code> as in the following code:</p>
  <pre><code>//Package, Import...
  
  @Entity
  @Table(name=&quot;COUNTRY&quot;)
  public class Country implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name=&quot;COU_ID&quot;)
      private Integer couID;
  
      @Column(name=&quot;LABEL&quot;)
      private String label;
  
      //bi-directional many-to-many association to TFilmFil
      @ManyToMany
      @JoinTable(
          name=&quot;COUNTRY_MOVIE&quot;
          , joinColumns={
              @JoinColumn(name=&quot;COU_ID&quot;)
              }
          , inverseJoinColumns={
              @JoinColumn(name=&quot;MO_ID&quot;)
              }
          )
      private List&lt;Movie&gt; movies;
  
      public Country() {
          super();
      }
      
      // Other constructor, Getter &amp; Setter, and &quot;toString&quot; method...
  }
  </code></pre>
  <h3>Movie</h3>
  <p>And in <code>Movie.java</code> I use only a <code>@ManyToMany</code> annotation :</p>
  <pre><code>//Package, Import...
  
  @Entity
  @Table(name=&quot;MOVIE&quot;)
  public class Movie implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name=&quot;MO_ID&quot;)
      private Integer moId;
  
      @Column(name=&quot;LABEL&quot;)
      private String label;
  
      //bi-directional many-to-many association to TrPaysPay
      @ManyToMany(mappedBy=&quot;movies&quot;)
      private List&lt;Country&gt; countries;
  
      public Film() {
          super();
      }
  
      // Other constructor, Getter &amp; Setter, and &quot;toString&quot; method...
  }
  </code></pre>
  <hr />
  <h3>Issue 1: <code>LazyInitializationException</code></h3>
  <p>I don't know if it's the best way to manage this kind of <em>association table</em> even if it looks OK.</p>
  <p>But when I try to get all the countries of a movie I obtain a <code>LazyInitializationException</code> from Hibernate.</p>
  <h3>Issue 2: After I tried <code>FetchType.EAGER</code></h3>
  <p>So I tried to add a fetch EAGER to each many-to-many annotation:</p>
  <p>In <code>Country</code>:</p>
  <pre><code>@ManyToMany(fetch=FetchType.EAGER)
  @JoinTable(
  </code></pre>
  <p>In <code>Movie</code>:</p>
  <pre><code>@ManyToMany(mappedBy=&quot;movies&quot;,fetch=FetchType.EAGER)
  </code></pre>
  <p>Then I obtain this :</p>
  <blockquote>
  <p>org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons</p>
  </blockquote>
  <h2>Questions</h2>
  <ol>
  <li>Do I have to create another java class to map <code>COUNTRY_MOVIE</code>?</li>
  <li>Or is there a way to fix this problem by using the same java class as now?</li>
  </ol>