Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions hibernate-core/src/main/java/org/hibernate/StatelessSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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> T get(Class<T> entityClass, Object id);

Expand All @@ -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);

Expand All @@ -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> T get(Class<T> entityClass, Object id, LockMode lockMode);

Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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() );
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down