Skip to content

Commit

Permalink
Merge pull request quarkusio#27666 from yrodiere/enableCollectionInDe…
Browse files Browse the repository at this point in the history
…faultFetchGroup

Always instantiate Hibernate ORM collections in the default fetch group
  • Loading branch information
gsmet authored Sep 7, 2022
2 parents 09490b7 + 2f77645 commit 036a69f
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 16 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.hibernate.orm.publicfields;
package io.quarkus.hibernate.orm.applicationfieldaccess;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -30,8 +30,8 @@
import io.quarkus.test.QuarkusUnitTest;

/**
* Checks that public field access is correctly replaced with getter/setter calls,
* regardless of the field type.
* Checks that access to public fields by the application is correctly replaced with getter/setter calls
* and works correctly for all association types.
*/
public class PublicFieldAccessAssociationsTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.hibernate.orm.publicfields;
package io.quarkus.hibernate.orm.applicationfieldaccess;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -23,8 +23,8 @@
import io.quarkus.test.QuarkusUnitTest;

/**
* Checks that public field access is correctly replaced with getter/setter calls,
* regardless of the field type.
* Checks that access to public fields by the application is correctly replaced with getter/setter calls
* and works correctly for all field types.
*/
public class PublicFieldAccessFieldTypesTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package io.quarkus.hibernate.orm.publicfields;
package io.quarkus.hibernate.orm.applicationfieldaccess;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -30,7 +30,7 @@
import io.quarkus.test.QuarkusUnitTest;

/**
* Checks that public, final field access is correctly replaced with getter calls for reads,
* Checks that access to public, final fields by the application is correctly replaced with getter calls for reads,
* but not replaced at all for writes (since writes to final fields can only occur from constructors).
* <p>
* See https://github.com/quarkusio/quarkus/issues/20186
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.hibernate.orm.publicfields;
package io.quarkus.hibernate.orm.applicationfieldaccess;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -22,8 +22,8 @@
import io.quarkus.test.QuarkusUnitTest;

/**
* Checks that public field access is correctly replaced with getter/setter calls,
* regardless of where the field is declared in the class hierarchy.
* Checks that access to public fields by the application is correctly replaced with getter/setter calls
* and works correctly regardless of where the field is declared in the class hierarchy.
*/
public class PublicFieldAccessInheritanceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package io.quarkus.hibernate.orm.publicfields;
package io.quarkus.hibernate.orm.applicationfieldaccess;

import static io.quarkus.hibernate.orm.TransactionTestUtils.inTransaction;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ public void testDefaultBatchFetchSize() throws Exception {

transaction.begin();
List<MainEntity> entities = session.createQuery("from MainEntity", MainEntity.class).list();
assertThat(entities).allSatisfy(e -> assertThat(Hibernate.isPropertyInitialized(e, "others"))
assertThat(entities).allSatisfy(e -> assertThat(Hibernate.isInitialized(e.others))
.as("'others' initialized for " + e).isFalse());

MainEntity entity = entities.get(0);
// Trigger initialization for the collection from one entity.
entity.others.get(0);
assertThat(Hibernate.isPropertyInitialized(entity, "others"))
assertThat(Hibernate.isInitialized(entity.others))
.as("'others' initialized for " + entity).isTrue();

// 20 entities were already in the session when the initialization above occurred,
// so it should have triggered batch initialization of several 'others' collections.
assertThat(entities).hasSize(20);
assertThat(entities)
.filteredOn(e -> Hibernate.isPropertyInitialized(e, "others"))
.filteredOn(e -> Hibernate.isInitialized(e.others))
.hasSize(16); // Default batch fetch size is 16
transaction.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ private String getExceptionHeader() {
}

protected void populate(String persistenceUnitName, SessionFactoryOptionsBuilder options, StandardServiceRegistry ssr) {

// will use user override value or default to false if not supplied to follow
// JPA spec.
final boolean jtaTransactionAccessEnabled = runtimeSettings.getBoolean(
Expand Down Expand Up @@ -158,6 +157,11 @@ protected void populate(String persistenceUnitName, SessionFactoryOptionsBuilder

options.applyEntityNotFoundDelegate(new JpaEntityNotFoundDelegate());

// This is necessary for Hibernate Reactive, see https://github.com/quarkusio/quarkus/issues/15814
// This is also necessary for Hibernate ORM if we want to prevent calls to getters on initialized entities
// outside of sessions from throwing exceptions, see https://github.com/quarkusio/quarkus/discussions/27657
options.enableCollectionInDefaultFetchGroup(true);

if (this.validatorFactory != null) {
options.applyValidatorFactory(validatorFactory);
}
Expand Down

0 comments on commit 036a69f

Please sign in to comment.