From 50271f8c8f7e08580e9610b8d5c641018e131017 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 30 May 2025 06:26:29 +0200 Subject: [PATCH] test and document behavior of SS.get() when entity not found --- .../java/org/hibernate/StatelessSession.java | 24 ++++++++++------ ...oxyFactoryWithSubclassesStatelessTest.java | 6 ++-- .../test/stateless/StatelessSessionTest.java | 28 +++++++++++++------ .../test/stateless/UpsertVersionedTest.java | 14 +++++----- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/StatelessSession.java b/hibernate-core/src/main/java/org/hibernate/StatelessSession.java index edb20f38cf0a..8a4a0ddacc87 100644 --- a/hibernate-core/src/main/java/org/hibernate/StatelessSession.java +++ b/hibernate-core/src/main/java/org/hibernate/StatelessSession.java @@ -240,7 +240,8 @@ public interface StatelessSession extends SharedSessionContract { * @param entityName The name of the entity to retrieve * @param id The id of the entity to retrieve * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id */ Object get(String entityName, Object id); @@ -250,7 +251,8 @@ public interface StatelessSession extends SharedSessionContract { * @param entityClass The class of the entity to retrieve * @param id The id of the entity to retrieve * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id */ T get(Class entityClass, Object id); @@ -261,7 +263,8 @@ public interface StatelessSession extends SharedSessionContract { * @param id The id of the entity to retrieve * @param lockMode The lock mode to apply to the entity * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id */ Object get(String entityName, Object id, LockMode lockMode); @@ -272,7 +275,8 @@ public interface StatelessSession extends SharedSessionContract { * @param id The id of the entity to retrieve * @param lockMode The lock mode to apply to the entity * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id */ T get(Class entityClass, Object id, LockMode lockMode); @@ -285,7 +289,8 @@ public interface StatelessSession extends SharedSessionContract { * {@linkplain org.hibernate.graph.GraphSemantic#LOAD load graph} * @param id The id of the entity to retrieve * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id * * @since 7.0 */ @@ -302,7 +307,8 @@ public interface StatelessSession extends SharedSessionContract { * @param id The id of the entity to retrieve * @param lockMode The lock mode to apply to the entity * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id * * @since 7.0 */ @@ -317,7 +323,8 @@ public interface StatelessSession extends SharedSessionContract { * how the graph should be interpreted * @param id The id of the entity to retrieve * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id * * @since 6.3 */ @@ -334,7 +341,8 @@ public interface StatelessSession extends SharedSessionContract { * @param id The id of the entity to retrieve * @param lockMode The lock mode to apply to the entity * - * @return a detached entity instance + * @return a detached entity instance, or null if there + * is no instance with the given id * * @since 6.3 */ diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java index 8d3dd1c8f744..28f2e97909dc 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java @@ -73,7 +73,7 @@ public void testNewEnhancedProxyAssociation(SessionFactoryScope scope) { session -> { final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" ); + final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) ); assertFalse( Hibernate.isInitialized( otherEntity.human ) ); assertFalse( HibernateProxy.class.isInstance( otherEntity.animal ) ); @@ -102,7 +102,7 @@ public void testExistingInitializedAssociationLeafSubclass(SessionFactoryScope s scope.inStatelessSession( session -> { - final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" ); + final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); /* The original test used @@ -161,7 +161,7 @@ public void testExistingEnhancedProxyAssociationLeafSubclassOnly(SessionFactoryS final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" ); + final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertNull( otherEntity.animal ); assertNull( otherEntity.primate ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java index 1cd2ca57b1ce..ca13effe6d9d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java @@ -8,8 +8,8 @@ import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; +import org.hibernate.LockMode; import org.hibernate.ScrollMode; -import org.hibernate.ScrollableResults; import org.hibernate.Transaction; import org.hibernate.testing.orm.junit.DomainModel; @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Gavin King @@ -33,10 +34,7 @@ public class StatelessSessionTest { @AfterEach public void tearDown(SessionFactoryScope scope){ - scope.inTransaction( - session -> - session.createQuery( "delete from Document" ).executeUpdate() - ); + scope.getSessionFactory().getSchemaManager().truncate(); } @Test @@ -73,7 +71,7 @@ public void testCreateUpdateReadDelete(SessionFactoryScope scope) { assertEquals( "Blahs", doc2.getName() ); assertEquals( doc.getText(), doc2.getText() ); - try (ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" ) + try (var sr = statelessSession.createQuery( "from Document where text is not null" ) .scroll( ScrollMode.FORWARD_ONLY )) { sr.next(); doc2 = (Document) sr.get(); @@ -98,9 +96,9 @@ public void testCreateUpdateReadDelete(SessionFactoryScope scope) { criteria = criteriaBuilder.createQuery( Document.class ); criteria.from( Document.class ); - try (ScrollableResults sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) { + try (var sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) { sr.next(); - doc2 = (Document) sr.get(); + doc2 = sr.get(); } assertEquals( "Blahs", doc2.getName() ); assertEquals( doc.getText(), doc2.getText() ); @@ -118,6 +116,20 @@ public void testCreateUpdateReadDelete(SessionFactoryScope scope) { } ); } + @Test + public void testGetNull(SessionFactoryScope scope) { + scope.inStatelessSession( + statelessSession -> { + assertNull( statelessSession.get( Document.class, "Blank" ) ); + } + ); + scope.inStatelessTransaction( + statelessSession -> { + assertNull( statelessSession.get( Document.class, "Blank", LockMode.PESSIMISTIC_WRITE ) ); + } + ); + } + @Test public void testHqlBulk(SessionFactoryScope scope) { scope.inStatelessSession( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/UpsertVersionedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/UpsertVersionedTest.java index c08f714408c1..1438f4483a54 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/UpsertVersionedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stateless/UpsertVersionedTest.java @@ -12,7 +12,7 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; @SessionFactory @DomainModel(annotatedClasses = UpsertVersionedTest.Record.class) @@ -23,22 +23,22 @@ public class UpsertVersionedTest { s.upsert(new Record(456L,2L,"hello mars")); }); scope.inStatelessTransaction(s-> { - assertEquals("hello earth",s.get(Record.class,123L).message); - assertEquals("hello mars",s.get(Record.class,456L).message); + assertEquals( "hello earth", s.get( Record.class,123L).message ); + assertEquals( "hello mars", s.get( Record.class,456L).message ); }); scope.inStatelessTransaction(s-> { s.upsert(new Record(123L,0L,"goodbye earth")); }); scope.inStatelessTransaction(s-> { - assertEquals("goodbye earth",s.get(Record.class,123L).message); - assertEquals("hello mars",s.get(Record.class,456L).message); + assertEquals( "goodbye earth", s.get( Record.class,123L).message ); + assertEquals( "hello mars", s.get( Record.class,456L).message ); }); scope.inStatelessTransaction(s-> { s.upsert(new Record(456L,3L,"goodbye mars")); }); scope.inStatelessTransaction(s-> { - assertEquals("goodbye earth",s.get(Record.class,123L).message); - assertEquals("goodbye mars",s.get(Record.class,456L).message); + assertEquals( "goodbye earth", s.get( Record.class,123L).message ); + assertEquals( "goodbye mars", s.get( Record.class,456L).message ); }); } @Entity(name = "Record")