Skip to content

Commit cdbb8a0

Browse files
committed
test and document behavior of SS.get() when entity not found
1 parent 81d5254 commit cdbb8a0

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

hibernate-core/src/main/java/org/hibernate/StatelessSession.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ public interface StatelessSession extends SharedSessionContract {
240240
* @param entityName The name of the entity to retrieve
241241
* @param id The id of the entity to retrieve
242242
*
243-
* @return a detached entity instance
243+
* @return a detached entity instance, or null if there
244+
* is no instance with the given id
244245
*/
245246
Object get(String entityName, Object id);
246247

@@ -250,7 +251,8 @@ public interface StatelessSession extends SharedSessionContract {
250251
* @param entityClass The class of the entity to retrieve
251252
* @param id The id of the entity to retrieve
252253
*
253-
* @return a detached entity instance
254+
* @return a detached entity instance, or null if there
255+
* is no instance with the given id
254256
*/
255257
<T> T get(Class<T> entityClass, Object id);
256258

@@ -261,7 +263,8 @@ public interface StatelessSession extends SharedSessionContract {
261263
* @param id The id of the entity to retrieve
262264
* @param lockMode The lock mode to apply to the entity
263265
*
264-
* @return a detached entity instance
266+
* @return a detached entity instance, or null if there
267+
* is no instance with the given id
265268
*/
266269
Object get(String entityName, Object id, LockMode lockMode);
267270

@@ -272,7 +275,8 @@ public interface StatelessSession extends SharedSessionContract {
272275
* @param id The id of the entity to retrieve
273276
* @param lockMode The lock mode to apply to the entity
274277
*
275-
* @return a detached entity instance
278+
* @return a detached entity instance, or null if there
279+
* is no instance with the given id
276280
*/
277281
<T> T get(Class<T> entityClass, Object id, LockMode lockMode);
278282

@@ -285,7 +289,8 @@ public interface StatelessSession extends SharedSessionContract {
285289
* {@linkplain org.hibernate.graph.GraphSemantic#LOAD load graph}
286290
* @param id The id of the entity to retrieve
287291
*
288-
* @return a detached entity instance
292+
* @return a detached entity instance, or null if there
293+
* is no instance with the given id
289294
*
290295
* @since 7.0
291296
*/
@@ -302,7 +307,8 @@ public interface StatelessSession extends SharedSessionContract {
302307
* @param id The id of the entity to retrieve
303308
* @param lockMode The lock mode to apply to the entity
304309
*
305-
* @return a detached entity instance
310+
* @return a detached entity instance, or null if there
311+
* is no instance with the given id
306312
*
307313
* @since 7.0
308314
*/
@@ -317,7 +323,8 @@ public interface StatelessSession extends SharedSessionContract {
317323
* how the graph should be interpreted
318324
* @param id The id of the entity to retrieve
319325
*
320-
* @return a detached entity instance
326+
* @return a detached entity instance, or null if there
327+
* is no instance with the given id
321328
*
322329
* @since 6.3
323330
*/
@@ -334,7 +341,8 @@ public interface StatelessSession extends SharedSessionContract {
334341
* @param id The id of the entity to retrieve
335342
* @param lockMode The lock mode to apply to the entity
336343
*
337-
* @return a detached entity instance
344+
* @return a detached entity instance, or null if there
345+
* is no instance with the given id
338346
*
339347
* @since 6.3
340348
*/

hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void testNewEnhancedProxyAssociation(SessionFactoryScope scope) {
7373
session -> {
7474
final Statistics stats = scope.getSessionFactory().getStatistics();
7575
stats.clear();
76-
final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" );
76+
final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" );
7777
assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) );
7878
assertFalse( Hibernate.isInitialized( otherEntity.human ) );
7979
assertFalse( HibernateProxy.class.isInstance( otherEntity.animal ) );
@@ -102,7 +102,7 @@ public void testExistingInitializedAssociationLeafSubclass(SessionFactoryScope s
102102
scope.inStatelessSession(
103103
session -> {
104104

105-
final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" );
105+
final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" );
106106
assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) );
107107
/*
108108
The original test used
@@ -161,7 +161,7 @@ public void testExistingEnhancedProxyAssociationLeafSubclassOnly(SessionFactoryS
161161
final Statistics stats = scope.getSessionFactory().getStatistics();
162162
stats.clear();
163163

164-
final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" );
164+
final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" );
165165
assertNull( otherEntity.animal );
166166
assertNull( otherEntity.primate );
167167
assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) );

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/StatelessSessionTest.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import jakarta.persistence.criteria.CriteriaBuilder;
99
import jakarta.persistence.criteria.CriteriaQuery;
1010

11+
import org.hibernate.LockMode;
1112
import org.hibernate.ScrollMode;
12-
import org.hibernate.ScrollableResults;
1313
import org.hibernate.Transaction;
1414

1515
import org.hibernate.testing.orm.junit.DomainModel;
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertNotSame;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
2425

2526
/**
2627
* @author Gavin King
@@ -33,10 +34,7 @@ public class StatelessSessionTest {
3334

3435
@AfterEach
3536
public void tearDown(SessionFactoryScope scope){
36-
scope.inTransaction(
37-
session ->
38-
session.createQuery( "delete from Document" ).executeUpdate()
39-
);
37+
scope.getSessionFactory().getSchemaManager().truncate();
4038
}
4139

4240
@Test
@@ -73,7 +71,7 @@ public void testCreateUpdateReadDelete(SessionFactoryScope scope) {
7371
assertEquals( "Blahs", doc2.getName() );
7472
assertEquals( doc.getText(), doc2.getText() );
7573

76-
try (ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" )
74+
try (var sr = statelessSession.createQuery( "from Document where text is not null" )
7775
.scroll( ScrollMode.FORWARD_ONLY )) {
7876
sr.next();
7977
doc2 = (Document) sr.get();
@@ -98,9 +96,9 @@ public void testCreateUpdateReadDelete(SessionFactoryScope scope) {
9896
criteria = criteriaBuilder.createQuery( Document.class );
9997
criteria.from( Document.class );
10098

101-
try (ScrollableResults sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) {
99+
try (var sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) {
102100
sr.next();
103-
doc2 = (Document) sr.get();
101+
doc2 = sr.get();
104102
}
105103
assertEquals( "Blahs", doc2.getName() );
106104
assertEquals( doc.getText(), doc2.getText() );
@@ -118,6 +116,20 @@ public void testCreateUpdateReadDelete(SessionFactoryScope scope) {
118116
} );
119117
}
120118

119+
@Test
120+
public void testGetNull(SessionFactoryScope scope) {
121+
scope.inStatelessSession(
122+
statelessSession -> {
123+
assertNull( statelessSession.get( Document.class, "Blank" ) );
124+
}
125+
);
126+
scope.inStatelessTransaction(
127+
statelessSession -> {
128+
assertNull( statelessSession.get( Document.class, "Blank", LockMode.PESSIMISTIC_WRITE ) );
129+
}
130+
);
131+
}
132+
121133
@Test
122134
public void testHqlBulk(SessionFactoryScope scope) {
123135
scope.inStatelessSession(

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/UpsertVersionedTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.hibernate.testing.orm.junit.SessionFactoryScope;
1313
import org.junit.jupiter.api.Test;
1414

15-
import static org.junit.Assert.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
1616

1717
@SessionFactory
1818
@DomainModel(annotatedClasses = UpsertVersionedTest.Record.class)
@@ -23,22 +23,22 @@ public class UpsertVersionedTest {
2323
s.upsert(new Record(456L,2L,"hello mars"));
2424
});
2525
scope.inStatelessTransaction(s-> {
26-
assertEquals("hello earth",s.get(Record.class,123L).message);
27-
assertEquals("hello mars",s.get(Record.class,456L).message);
26+
assertEquals( "hello earth", s.get( Record.class,123L).message );
27+
assertEquals( "hello mars", s.get( Record.class,456L).message );
2828
});
2929
scope.inStatelessTransaction(s-> {
3030
s.upsert(new Record(123L,0L,"goodbye earth"));
3131
});
3232
scope.inStatelessTransaction(s-> {
33-
assertEquals("goodbye earth",s.get(Record.class,123L).message);
34-
assertEquals("hello mars",s.get(Record.class,456L).message);
33+
assertEquals( "goodbye earth", s.get( Record.class,123L).message );
34+
assertEquals( "hello mars", s.get( Record.class,456L).message );
3535
});
3636
scope.inStatelessTransaction(s-> {
3737
s.upsert(new Record(456L,3L,"goodbye mars"));
3838
});
3939
scope.inStatelessTransaction(s-> {
40-
assertEquals("goodbye earth",s.get(Record.class,123L).message);
41-
assertEquals("goodbye mars",s.get(Record.class,456L).message);
40+
assertEquals( "goodbye earth", s.get( Record.class,123L).message );
41+
assertEquals( "goodbye mars", s.get( Record.class,456L).message );
4242
});
4343
}
4444
@Entity(name = "Record")

0 commit comments

Comments
 (0)