title: Hibernate throws StackOverflowError when querying
body: <p>I have two entities defined as so:</p>
  
  <pre><code>@Entity
  @Table(name = "shapes")
  public class Shape {
      @Id
      private int id;
  
      @Column(nullable = false)
      private String name;
  
      @OneToMany(mappedBy="shape", fetch = FetchType.EAGER)
      private List&lt;ShapeElement&gt; shapeElements;
  
      public int getId() {
          return id;
      }
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>@Entity
  @IdClass(ShapeElementPK.class)
  @Table(name = "shape_elements")
  public class ShapeElement {
      @Id
      @ManyToOne
      @JoinColumn(name = "shape_id")
      private Shape shape;
  
      @Id
      @Column(name = "x_delta")
      private int xDelta;
  
      @Id
      @Column(name = "y_delta")
      private int yDelta;
  }
  </code></pre>
  
  <p>With the following IdClass:</p>
  
  <pre><code>@Embeddable
  public class ShapeElementPK implements Serializable {
      private static final long serialVersionUID = 284329766961384662L;
  
      private Shape shape;
      private int xDelta;
      private int yDelta;
  
      @Override
      public boolean equals(Object otherObject) {
          if (! (otherObject instanceof ShapeElementPK)) {
              return false;
          }
  
          ShapeElementPK otherPK = (ShapeElementPK) otherObject;
          return otherPK.shape.getId() == shape.getId() &amp;&amp;
                  otherPK.xDelta == xDelta &amp;&amp;
                  otherPK.yDelta == yDelta;
      }
  
      @Override
      public int hashCode() {
          int hash = 5;
          hash = 11 * hash + Objects.hashCode(this.shape);
          hash = 11 * hash + this.xDelta;
          hash = 11 * hash + this.yDelta;
          return hash;
      }
  }
  </code></pre>
  
  <p>Now when I run</p>
  
  <pre><code>List&lt;Shape&gt; allShapes = session.createCriteria(Shape.class).list();
  </code></pre>
  
  <p>I end up with a StackOverflowError with the following back trace:</p>
  
  <pre><code>Caused by: java.lang.StackOverflowError
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at com.mysql.jdbc.StatementImpl.getWarnings(StatementImpl.java:2556)
      at com.zaxxer.hikari.proxy.PreparedStatementJavassistProxy.getWarnings(PreparedStatementJavassistProxy.java)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.handleAndClearWarnings(SqlExceptionHelper.java:320)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logAndClearWarnings(SqlExceptionHelper.java:273)
      at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:529)
      at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.release(JdbcCoordinatorImpl.java:421)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:160)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
      at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at com.mysql.jdbc.StatementImpl.getWarnings(StatementImpl.java:2556)
      at com.zaxxer.hikari.proxy.PreparedStatementJavassistProxy.getWarnings(PreparedStatementJavassistProxy.java)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.handleAndClearWarnings(SqlExceptionHelper.java:320)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logAndClearWarnings(SqlExceptionHelper.java:273)
      at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:529)
      at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.release(JdbcCoordinatorImpl.java:421)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:160)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
      at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
      at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
      at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
      at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
      at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
      at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
      at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
      at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
      at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:989)
      at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716)
      at org.hibernate.type.EntityType.resolve(EntityType.java:502)
      at org.hibernate.type.ComponentType.resolve(ComponentType.java:666)
      at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.resolveEntityKey(EntityReferenceInitializerImpl.java:158)
      at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.resolveEntityKey(AbstractRowReader.java:148)
      at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:97)
      at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:255)
      at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:129)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
      at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
      at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
      at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
      at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
      at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
      at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
      at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
      at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
      at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:989)
      at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716)
      at org.hibernate.type.EntityType.resolve(EntityType.java:502)
      at org.hibernate.type.ComponentType.resolve(ComponentType.java:666)
      .... etc ... it keeps repeating like this ...
  </code></pre>
  
  <p>What am I doing wrong?  This is with Hibernate 4.3.6.Final.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I changed the ShapeElementPK to the following, and it seems to have solved the issue:</p>
  
  <pre><code>@Embeddable
  public class ShapeElementPK implements Serializable {
      private static final long serialVersionUID = 284329766961384662L;
  
      private int shape;
      private int xDelta;
      private int yDelta;
  
      @Override
      public boolean equals(Object otherObject) {
          if (! (otherObject instanceof ShapeElementPK)) {
              return false;
          }
  
          ShapeElementPK otherPK = (ShapeElementPK) otherObject;
          return otherPK.shape == shape &amp;&amp;
                  otherPK.xDelta == xDelta &amp;&amp;
                  otherPK.yDelta == yDelta;
      }
  
      @Override
      public int hashCode() {
          int hash = 5;
          hash = 11 * hash + this.shape;
          hash = 11 * hash + this.xDelta;
          hash = 11 * hash + this.yDelta;
          return hash;
      }
  }
  </code></pre>
  
  <p>The change was to define shape as an int instead of a Shape in the ShapeElementPK.</p>