title: Stackoverflowerror / Unresolved Object Exception with Hibernate, Collections and Session.refresh(...)
body: <p>In my current project, I'm using Hibernate 3. The tool I'm working on connects to a MS SQL Server Database. The tool is installed on two different Machines, both connect to the same Database. </p>
  
  <p>On the first machine I'm altering an object that contains a collection. On the second Machine I'm using session.refresh(...) to see the Changes made on the first Machine. </p>
  
  <p>When I add an Object to the collection and refresh, I get a Stackoverflowerror. When I remove an Object from the collection and refresh, I get an UnresolvedObjectException. </p>
  
  <pre><code>session.evict(...)
  session.get(...) 
  </code></pre>
  
  <p>seems to work. But <code>session.refresh(...)</code>. Why?</p>
  
  <p>Here is a full stacktrace:</p>
  
  <pre><code>java.lang.StackOverflowError
  at net.sourceforge.jtds.jdbc.JtdsStatement.&lt;init&gt;(JtdsStatement.java:129)
  at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.&lt;init&gt;(JtdsPreparedStatement.java:99)
  at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareStatement(ConnectionJDBC2.java:2456)
  at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareStatement(ConnectionJDBC2.java:2414)
  at net.sourceforge.jtds.jdbcx.proxy.ConnectionProxy.prepareStatement(ConnectionProxy.java:394)
  at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
  at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
  at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
  at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
  at org.hibernate.loader.Loader.doQuery(Loader.java:696)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:588)
  at org.hibernate.loader.Loader.doQuery(Loader.java:724)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
  at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
  at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
  at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
  at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
  at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
  at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
  at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
  at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
  at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
  at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
  at org.hibernate.type.EntityType.resolve(EntityType.java:412)
  at org.hibernate.type.ComponentType.resolve(ComponentType.java:607)
  at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:298)
  at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
  </code></pre>