From efe18b69850349e2a8f25f8dec2c001989cb146c Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 21 Mar 2025 19:19:54 +0100 Subject: [PATCH 1/3] fix infoString() --- .../java/org/hibernate/engine/spi/EntityKey.java | 5 +++-- .../java/org/hibernate/pretty/MessageHelper.java | 12 +++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java index 54acac4d0626..b7032632dc07 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityKey.java @@ -11,11 +11,12 @@ import org.hibernate.AssertionFailure; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.pretty.MessageHelper; import org.hibernate.type.Type; import org.checkerframework.checker.nullness.qual.Nullable; +import static org.hibernate.pretty.MessageHelper.infoString; + /** * Uniquely identifies of an entity instance in a particular Session by identifier. * Note that it's only safe to be used within the scope of a Session: it doesn't consider for example the tenantId @@ -117,7 +118,7 @@ public int hashCode() { @Override public String toString() { - return "EntityKey" + MessageHelper.infoString( this.persister, identifier, persister.getFactory() ); + return "EntityKey" + infoString( this.persister, identifier, persister.getFactory() ); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java b/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java index 9e42a0e1ccbc..aff4b6ae2bd8 100644 --- a/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/pretty/MessageHelper.java @@ -87,17 +87,15 @@ public static String infoString( info.append( " with null id" ); } else { - info.append( " with id '" ).append( id ).append( "'" ); + info.append( " with id '" ); if ( idType == null ) { info.append( id ); } + else if ( factory != null ) { + info.append( idType.toLoggableString( id, factory ) ); + } else { - if ( factory != null ) { - info.append( idType.toLoggableString( id, factory ) ); - } - else { - info.append( "" ); - } + info.append( "" ); } info.append( "'" ); } From 597bbe4c186ce031623fe433a90f257555db7cec Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 21 Mar 2025 19:20:51 +0100 Subject: [PATCH 2/3] deprecate unused SPI method --- .../org/hibernate/engine/spi/SessionDelegatorBaseImpl.java | 2 +- .../hibernate/engine/spi/SharedSessionContractImplementor.java | 3 +++ .../hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java | 2 +- .../src/main/java/org/hibernate/internal/SessionImpl.java | 2 +- .../main/java/org/hibernate/internal/StatelessSessionImpl.java | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index db83ab70338c..7e58fdee2be7 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -198,7 +198,7 @@ public String guessEntityName(Object entity) throws HibernateException { return delegate.guessEntityName( entity ); } - @Override + @Override @Deprecated public Object instantiate(String entityName, Object id) throws HibernateException { return delegate.instantiate( entityName, id ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java index 20b891975dd2..6ae7a2a9fd23 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java @@ -353,7 +353,10 @@ default String bestGuessEntityName(Object object, EntityEntry entry) { /** * Instantiate the entity class, initializing with the given identifier. + * + * @deprecated No longer used, replaced by {@link #instantiate(EntityPersister, Object)} */ + @Deprecated(since = "7", forRemoval = true) Object instantiate(String entityName, Object id) throws HibernateException; /** diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java index 10a297ddca00..452989a79525 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java @@ -456,7 +456,7 @@ public String guessEntityName(Object entity) throws HibernateException { return delegate.guessEntityName( entity ); } - @Override + @Override @Deprecated public Object instantiate(String entityName, Object id) throws HibernateException { return delegate.instantiate( entityName, id ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 475d01ae0685..8f384528533f 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -1524,7 +1524,7 @@ public void forceFlush(EntityKey key) { doFlush(); } - @Override + @Override @Deprecated public Object instantiate(String entityName, Object id) { return instantiate( requireEntityPersister( entityName ), id ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index cdc70e3adc70..97a3c875cfb8 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -890,7 +890,7 @@ public void initializeCollection(PersistentCollection collection, boolean wri } } - @Override + @Override @Deprecated public Object instantiate(String entityName, Object id) { return instantiate( requireEntityPersister( entityName ), id ); } From b983944d74e2d6239eb266801330a34d73578e32 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 21 Mar 2025 19:21:28 +0100 Subject: [PATCH 3/3] work on tests for batch fetching --- .../lazy/proxy/BatchFetchProxyTest.java | 34 +++++++++++++++++++ .../orm/test/fetching/BatchFetchingTest.java | 3 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java index 05fab5a0dd00..e143748f9851 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java @@ -197,6 +197,40 @@ public void testBatchEntityLoadThenModify(SessionFactoryScope scope) { ); } + @Test + @JiraKey("HHH-11147") + public void testBatchEntityRemoval(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + final Statistics statistics = scope.getSessionFactory().getStatistics(); + statistics.clear(); + + List employers = new ArrayList<>(); + for ( int i = 0 ; i < 5 ; i++ ) { + employers.add( session.getReference( Employer.class, i + 1) ); + } + + assertEquals( 0, statistics.getPrepareStatementCount() ); + + session.find( Employer.class, 0 ); + session.find( Employer.class, 1 ); + session.find( Employer.class, 2 ); + session.find( Employer.class, 5 ); + + assertEquals( 1, statistics.getPrepareStatementCount() ); + + session.find( Employer.class, 6 ); + session.find( Employer.class, 7 ); + + assertEquals( 3, statistics.getPrepareStatementCount() ); + + for ( Employer employer : employers ) { + assertTrue( Hibernate.isInitialized( employer ) ); + } + } + ); + } + @BeforeEach public void setUpData(SessionFactoryScope scope) { scope.inTransaction( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/fetching/BatchFetchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/fetching/BatchFetchingTest.java index 960a7bd98405..276541017d47 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/fetching/BatchFetchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/fetching/BatchFetchingTest.java @@ -12,6 +12,7 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; +import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.NaturalId; import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; @@ -80,7 +81,7 @@ public static class Department { private Long id; @OneToMany(mappedBy = "department") - //@BatchSize(size = 5) + @BatchSize(size = 5) private List employees = new ArrayList<>(); //Getters and setters omitted for brevity