title: Hibernate causes infinite loop with @ManyToMany bidirectional relation
body: <p>I have two entities in my project with have a many-to-many relation between them. The relation is represented using a helper mapping table in postgres which contains the relevant ID for each entity, pretty standard.</p>
  
  <p>Let's call the table <code>bucket_object_mapping</code> and it has two columns: <code>bucket_id</code>, <code>object_id</code>.</p>
  
  <p>The bucket class:</p>
  
  <pre><code>@Entity
  @Table(name = "bucket")
  public class Bucket {
     // Some fields omitted here
  
     @ManyToMany(cascade = {}, fetch = FetchType.LAZY)
     @JoinTable(
        name = "bucket_object_mapping",
        joinColumns = @JoinColumn(name = "bucket_id"),
        inverseJoinColumns = @JoinColumn(name = "object_id")
     )
     private List&lt;Model&gt; models;
  }
  </code></pre>
  
  <p><strong>In the <code>Model</code> class I have no mapping to the <code>Bucket</code> class (no need for that)</strong></p>
  
  <p>When I run this simple query:</p>
  
  <pre><code>SELECT bucket FROM Bucket bucket WHERE bucket.customerId=:customerId
  </code></pre>
  
  <p>The data is returned fine at first, but then an infinite loop of queries to the <code>Model</code> table starts to run and I'm not sure why. Looks like it's happening when I return the result list through a SpringMVC controller. <strong>I do convert the <code>Bucket</code> to a DTO but not the <code>Model</code></strong> (It's just that the <code>Model</code> entity does not have any reference to the <code>Bucket</code> so there's not supposed to be a circular reference problem).</p>
  
  <p>Controller code:</p>
  
  <pre><code>@RequestMapping(value = "", method = RequestMethod.GET)
  @ResponseBody
  public List&lt;BucketDTO&gt; getAllByCustomer(@RequestParam(value = "customerid") final Long customerId) {
    return bucketService.getAllBucketsForCustomer(customerId);
  }
  </code></pre>
  
  <p>The code in the service:</p>
  
  <pre><code>@Transactional
  public List&lt;BucketDTO&gt; getAllBucketsForCustomer(final Long customerId) {
    List&lt;Bucket&gt; buckets = bucketDao.getBucketsForCustomer(customerId);
    List&lt;BucketDTO&gt; result = bucketDtoConverter.toDtoList(buckets);
    return result
  }
  </code></pre>