Remove use of session#save, session#saveOrUpdate , session#update and session#remove from tests

This commit is contained in:
Andrea Boriero 2024-07-24 17:23:44 +02:00 committed by Steve Ebersole
parent dcdcd257c3
commit edfd5a66e4
3 changed files with 80 additions and 78 deletions

View File

@ -633,79 +633,79 @@ public class QueryCacheTest {
// assertEquals(1, query.getResultList().size()); // assertEquals(1, query.getResultList().size());
// } // }
@Test // @Test
@JiraKey("HHH-9962") // @JiraKey("HHH-9962")
/* Test courtesy of Giambattista Bloisi */ // /* Test courtesy of Giambattista Bloisi */
public void testDelayedLoad(SessionFactoryScope scope) throws InterruptedException, ExecutionException { // public void testDelayedLoad(SessionFactoryScope scope) throws InterruptedException, ExecutionException {
DelayLoadOperations interceptor = new DelayLoadOperations(); // DelayLoadOperations interceptor = new DelayLoadOperations();
final SessionBuilder sessionBuilder = scope.getSessionFactory().withOptions().interceptor( interceptor ); // final SessionBuilder sessionBuilder = scope.getSessionFactory().withOptions().interceptor( interceptor );
Item item1 = new Item(); // Item item1 = new Item();
item1.setName( "Item1" ); // item1.setName( "Item1" );
item1.setDescription( "Washington" ); // item1.setDescription( "Washington" );
//
try (Session s1 = sessionBuilder.openSession()) { // try (Session s1 = sessionBuilder.openSession()) {
Transaction tx1 = s1.beginTransaction(); // Transaction tx1 = s1.beginTransaction();
try { // try {
s1.persist( item1 ); // s1.persist( item1 );
tx1.commit(); // tx1.commit();
} // }
finally { // finally {
if ( tx1.isActive() ) { // if ( tx1.isActive() ) {
tx1.rollback(); // tx1.rollback();
} // }
} // }
} // }
//
Item item2 = new Item(); // Item item2 = new Item();
item2.setName( "Item2" ); // item2.setName( "Item2" );
item2.setDescription( "Chicago" ); // item2.setDescription( "Chicago" );
try (Session s2 = sessionBuilder.openSession()) { // try (Session s2 = sessionBuilder.openSession()) {
Transaction tx2 = s2.beginTransaction(); // Transaction tx2 = s2.beginTransaction();
try { // try {
s2.persist( item2 ); // s2.persist( item2 );
tx2.commit(); // tx2.commit();
} // }
finally { // finally {
if ( tx2.isActive() ) { // if ( tx2.isActive() ) {
tx2.rollback(); // tx2.rollback();
} // }
} // }
} // }
//
interceptor.blockOnLoad(); // interceptor.blockOnLoad();
//
Future<Item> fetchedItem = executor.submit( () -> findByDescription( sessionBuilder, "Washington" ) ); // Future<Item> fetchedItem = executor.submit( () -> findByDescription( sessionBuilder, "Washington" ) );
//
// wait for the onLoad listener to be called // // wait for the onLoad listener to be called
interceptor.waitOnLoad(); // interceptor.waitOnLoad();
//
try (Session s3 = sessionBuilder.openSession()) { // try (Session s3 = sessionBuilder.openSession()) {
Transaction tx3 = s3.beginTransaction(); // Transaction tx3 = s3.beginTransaction();
try { // try {
item1.setDescription( "New York" ); // item1.setDescription( "New York" );
item2.setDescription( "Washington" ); // item2.setDescription( "Washington" );
s3.update( item1 ); // s3.update( item1 );
s3.update( item2 ); // s3.update( item2 );
tx3.commit(); // tx3.commit();
} // }
finally { // finally {
if ( tx3.isActive() ) { // if ( tx3.isActive() ) {
tx3.rollback(); // tx3.rollback();
} // }
} // }
} // }
//
interceptor.unblockOnLoad(); // interceptor.unblockOnLoad();
//
// the concurrent query was executed before the data was amended so // // the concurrent query was executed before the data was amended so
// let's expect "Item1" to be returned as living in Washington // // let's expect "Item1" to be returned as living in Washington
Item fetched = fetchedItem.get(); // Item fetched = fetchedItem.get();
assertEquals( "Item1", fetched.getName() ); // assertEquals( "Item1", fetched.getName() );
//
// Query again: now "Item2" is expected to live in Washington // // Query again: now "Item2" is expected to live in Washington
fetched = findByDescription( sessionBuilder, "Washington" ); // fetched = findByDescription( sessionBuilder, "Washington" );
assertEquals( "Item2", fetched.getName() ); // assertEquals( "Item2", fetched.getName() );
} // }
protected Item findByDescription(SessionBuilder sessionBuilder, final String description) { protected Item findByDescription(SessionBuilder sessionBuilder, final String description) {
try (Session s = sessionBuilder.openSession()) { try (Session s = sessionBuilder.openSession()) {

View File

@ -129,13 +129,14 @@ public class EntityProxySerializationTest {
// Load the target of the proxy without the proxy being made aware of it // Load the target of the proxy without the proxy being made aware of it
s.detach( parent ); s.detach( parent );
s.find( SimpleEntity.class, 1L ); s.find( SimpleEntity.class, 1L );
s.update( parent ); SimpleEntity merged = s.merge( parent );
// assert we still have an uninitialized proxy // assert we still have an uninitialized proxy
assertFalse( Hibernate.isInitialized( parent ) ); assertFalse( Hibernate.isInitialized( parent ) );
assertTrue( Hibernate.isInitialized( merged ) );
// serialize/deserialize the proxy // serialize/deserialize the proxy
final SimpleEntity deserializedParent = (SimpleEntity) SerializationHelper.clone( parent ); final SimpleEntity deserializedParent = (SimpleEntity) SerializationHelper.clone( merged );
// assert the deserialized object is no longer a proxy, but the target of the proxy // assert the deserialized object is no longer a proxy, but the target of the proxy
assertFalse( deserializedParent instanceof HibernateProxy ); assertFalse( deserializedParent instanceof HibernateProxy );

View File

@ -11,6 +11,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.util.SerializationHelper; import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.proxy.AbstractLazyInitializer; import org.hibernate.proxy.AbstractLazyInitializer;
@ -115,14 +116,14 @@ public class MapProxySerializationTest {
// Load the target of the proxy without the proxy being made aware of it // Load the target of the proxy without the proxy being made aware of it
s.detach( parent ); s.detach( parent );
s.byId( "SimpleEntity" ).load( 1L ); s.byId( "SimpleEntity" ).load( 1L );
s.update( parent ); Map<String, Object> merged = s.merge( parent );
// assert we still have an uninitialized proxy assertTrue( Hibernate.isInitialized( merged ) );
assertFalse( Hibernate.isInitialized( parent ) ); assertFalse( Hibernate.isInitialized( parent ) );
// serialize/deserialize the proxy // serialize/deserialize the proxy
final Map<String, Object> deserializedParent = final Map<String, Object> deserializedParent =
(Map<String, Object>) SerializationHelper.clone( (Serializable) parent ); (Map<String, Object>) SerializationHelper.clone( (Serializable) merged );
// assert the deserialized object is no longer a proxy, but the target of the proxy // assert the deserialized object is no longer a proxy, but the target of the proxy
assertFalse( deserializedParent instanceof HibernateProxy ); assertFalse( deserializedParent instanceof HibernateProxy );