title: How to delete records from two tables with `@OneToMany` relation?
body: <p>I am using JPA and Spring Data to perform CRUD operations. Find below the details:</p>
  
  <p><strong>Primary table:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "university")
  public class University implements Serializable {
      @OneToMany(mappedBy = "pk.university", 
                 cascade = CascadeType.ALL, 
                 fetch = FetchType.EAGER)
      private Set&lt;Student&gt; tagDetailReaders = new HashSet&lt;Student&gt;(0);
  }
  </code></pre>
  
  <p><strong>Foreign key table:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "student")
  @AssociationOverrides({ @AssociationOverride(name = "pk.university", joinColumns = @JoinColumn(name = "university_id")) })
  public class Student implements Serializable {
  
  }
  </code></pre>
  
  <p><strong>Spring data code to delete the records:</strong></p>
  
  <pre><code>University entity = universityDao.findAll().get(0);
  universityDao.delete(entity);
  </code></pre>
  
  <p><strong>I am getting following error:</strong></p>
  
  <pre><code>java.lang.StackOverflowError
      at java.net.DualStackPlainSocketImpl.socketAvailable(Unknown Source)
      at java.net.AbstractPlainSocketImpl.available(Unknown Source)
      at java.net.SocketInputStream.available(Unknown Source)
      at com.mysql.jdbc.util.ReadAheadInputStream.available(ReadAheadInputStream.java:230)
      at com.mysql.jdbc.MysqlIO.clearInputStream(MysqlIO.java:954)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2531)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
      at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
      at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
      at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
      at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
      at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
      at org.hibernate.loader.Loader.doQuery(Loader.java:909)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:324)
      at org.hibernate.loader.Loader.loadEntity(Loader.java:2148)
      at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:78)
      at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:68)
      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)
  </code></pre>
  
  <p>Could somebody let me know why I am getting stackoverflow error?</p>
  
  <p><strong>NOTE:</strong> It works fine if I change fetch type from <code>EAGER</code> to <code>LAZY</code> and set <code>orphanRemoval=true</code>. Please let me know if this is the only way to fix this issue?</p>