HHH-14993 - EAGER non-inverse collection throws StackOverflowError if `max_fetch_depth` not set

This commit is contained in:
Steve Ebersole 2022-03-14 20:39:11 -05:00
parent 38cffd0c8f
commit 9335d3efbf
2 changed files with 23 additions and 35 deletions

View File

@ -16,6 +16,7 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.tool.schema.Action;
import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -25,6 +26,10 @@ import org.jboss.shrinkwrap.api.classloader.ShrinkWrapClassLoader;
import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.jboss.shrinkwrap.api.spec.JavaArchive;
import static jakarta.persistence.Persistence.createEntityManagerFactory; import static jakarta.persistence.Persistence.createEntityManagerFactory;
import static org.hibernate.cfg.AvailableSettings.CLASSLOADERS;
import static org.hibernate.cfg.AvailableSettings.FORMAT_SQL;
import static org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO;
import static org.hibernate.cfg.AvailableSettings.MAX_FETCH_DEPTH;
import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction; import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction;
/** /**
@ -54,18 +59,10 @@ public class NoDepthTests {
private static SessionFactoryImplementor buildSessionFactory(boolean configureMax) { private static SessionFactoryImplementor buildSessionFactory(boolean configureMax) {
final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(); final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
registryBuilder.applySetting( AvailableSettings.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;" ); registryBuilder.applySetting( FORMAT_SQL, "true" );
registryBuilder.applySetting( AvailableSettings.USER, "sa" ); registryBuilder.applySetting( HBM2DDL_AUTO, Action.CREATE_DROP );
registryBuilder.applySetting( AvailableSettings.POOL_SIZE, "5" );
registryBuilder.applySetting( AvailableSettings.FORMAT_SQL, "true" );
registryBuilder.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
if ( configureMax ) { registryBuilder.applySetting( MAX_FETCH_DEPTH, configureMax ? "10" : "" );
registryBuilder.applySetting( AvailableSettings.MAX_FETCH_DEPTH, "10" );
}
else {
registryBuilder.applySetting( AvailableSettings.MAX_FETCH_DEPTH, "" );
}
return new MetadataSources( registryBuilder.build() ) return new MetadataSources( registryBuilder.build() )
.addAnnotatedClasses( SysModule.class, SysModule2.class ) .addAnnotatedClasses( SysModule.class, SysModule2.class )
@ -76,26 +73,28 @@ public class NoDepthTests {
@Test @Test
public void testWithMaxJpa() { public void testWithMaxJpa() {
testItJpa( "with-max" ); testItJpa( true );
} }
@Test @Test
public void testNoMaxJpa() { public void testNoMaxJpa() {
testItJpa( "no-max" ); testItJpa( false );
} }
private void testItJpa(String unitName) { private void testItJpa(boolean configureMax) {
final JavaArchive par = ShrinkWrap.create( JavaArchive.class, unitName + ".par" ); final JavaArchive par = ShrinkWrap.create( JavaArchive.class, "fetch-depth.par" );
par.addClasses( SysModule.class ); par.addClasses( SysModule.class );
par.addAsResource( "units/many2many/fetch-depth.xml", "META-INF/persistence.xml" ); par.addAsResource( "units/many2many/fetch-depth.xml", "META-INF/persistence.xml" );
try ( final ShrinkWrapClassLoader classLoader = new ShrinkWrapClassLoader( par ) ) { try ( final ShrinkWrapClassLoader classLoader = new ShrinkWrapClassLoader( par ) ) {
final Map<String, ?> settings = CollectionHelper.toMap( final Map<String, ?> settings = CollectionHelper.toMap(
AvailableSettings.CLASSLOADERS, CLASSLOADERS, Arrays.asList( classLoader, getClass().getClassLoader() ),
Arrays.asList( classLoader, getClass().getClassLoader() ) MAX_FETCH_DEPTH, configureMax ? "10" : "",
HBM2DDL_AUTO, Action.CREATE_DROP,
FORMAT_SQL, "true"
); );
final EntityManagerFactory emf = createEntityManagerFactory( unitName, settings ); final EntityManagerFactory emf = createEntityManagerFactory( "fetch-depth", settings );
try ( final SessionFactoryImplementor sf = emf.unwrap( SessionFactoryImplementor.class ) ) { try ( final SessionFactoryImplementor sf = emf.unwrap( SessionFactoryImplementor.class ) ) {
// play around with the SF and make sure it is operable // play around with the SF and make sure it is operable
inTransaction( sf, (s) -> { inTransaction( sf, (s) -> {

View File

@ -3,37 +3,26 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"> xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
<persistence-unit name="no-max" transaction-type="RESOURCE_LOCAL" > <persistence-unit name="fetch-depth" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.mapping.fetch.depth.SysModule</class> <class>org.hibernate.orm.test.mapping.fetch.depth.SysModule</class>
<class>org.hibernate.orm.test.mapping.fetch.depth.SysModule2</class> <class>org.hibernate.orm.test.mapping.fetch.depth.SysModule2</class>
</persistence-unit>
<persistence-unit name="no-max" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.mapping.fetch.depth.SysModule</class>
<class>org.hibernate.orm.test.mapping.fetch.depth.SysModule2</class>
<properties> <properties>
<property name="hibernate.connection.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.pool_size" value="5"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.max_fetch_depth" value="" /> <property name="hibernate.max_fetch_depth" value="" />
</properties> </properties>
</persistence-unit> </persistence-unit>
<persistence-unit name="with-max" transaction-type="RESOURCE_LOCAL" > <persistence-unit name="with-max" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.mapping.fetch.depth.SysModule</class> <class>org.hibernate.orm.test.mapping.fetch.depth.SysModule</class>
<class>org.hibernate.orm.test.mapping.fetch.depth.SysModule2</class> <class>org.hibernate.orm.test.mapping.fetch.depth.SysModule2</class>
<properties> <properties>
<property name="hibernate.connection.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.pool_size" value="5"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.max_fetch_depth" value="10" /> <property name="hibernate.max_fetch_depth" value="10" />
</properties> </properties>
</persistence-unit> </persistence-unit>