Merge branch 'master' into wip/6.0

This commit is contained in:
Andrea Boriero 2020-11-17 15:12:28 +01:00
commit 78938d0786
2 changed files with 112 additions and 73 deletions

View File

@ -25,6 +25,7 @@ import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.TestForIssue;
@ -40,11 +41,13 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
@ -105,8 +108,11 @@ public class MultiLoadTest implements SessionFactoryProducer {
List<SimpleEntity> list = session.byMultipleIds( SimpleEntity.class ).multiLoad( ids( 5 ) );
assertEquals( 5, list.size() );
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?, ?, ?, ?, ?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 5 ) );
}
);
}
@ -128,7 +134,7 @@ public class MultiLoadTest implements SessionFactoryProducer {
assertNull( s5 );
// finally assert how multiLoad handles it
List<SimpleEntity> list = session.byMultipleIds( SimpleEntity.class ).multiLoad( ids(56) );
List<SimpleEntity> list = session.byMultipleIds( SimpleEntity.class ).multiLoad( ids( 56 ) );
assertEquals( 56, list.size() );
}
);
@ -213,8 +219,9 @@ public class MultiLoadTest implements SessionFactoryProducer {
@TestForIssue(jiraKey = "HHH-12944")
@FailureExpected( reason = "Caching/CacheMode supported not yet implemented" )
public void testMultiLoadFrom2ndLevelCache(SessionFactoryScope scope) {
Statistics statistics = scope.getSessionFactory().getStatistics();
scope.getSessionFactory().getCache().evictAll();
final Statistics statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
@ -251,7 +258,11 @@ public class MultiLoadTest implements SessionFactoryProducer {
assertTrue( session.contains( entity ) );
}
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?, ?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 2 ) );
}
);
}
@ -260,9 +271,11 @@ public class MultiLoadTest implements SessionFactoryProducer {
@TestForIssue(jiraKey = "HHH-12944")
@FailureExpected( reason = "Caching/CacheMode supported not yet implemented" )
public void testUnorderedMultiLoadFrom2ndLevelCache(SessionFactoryScope scope) {
Statistics statistics = scope.getSessionFactory().getStatistics();
scope.getSessionFactory().getCache().evictAll();
final Statistics statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
// Load 1 of the items directly
@ -295,18 +308,20 @@ public class MultiLoadTest implements SessionFactoryProducer {
assertEquals( 3, entities.size() );
assertEquals( 1, statistics.getSecondLevelCacheHitCount() );
for(SimpleEntity entity: entities) {
for ( SimpleEntity entity : entities ) {
assertTrue( session.contains( entity ) );
}
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?, ?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 2 ) );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-12944")
@FailureExpected( reason = "Caching/CacheMode supported not yet implemented" )
public void testOrderedMultiLoadFrom2ndLevelCachePendingDelete(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
@ -314,7 +329,7 @@ public class MultiLoadTest implements SessionFactoryProducer {
sqlStatementInterceptor.getSqlQueries().clear();
// Multiload 3 items and ensure that multiload pulls 2 from the database & 1 from the cache.
// Multi-load 3 items and ensure that it pulls 2 from the database & 1 from the cache.
List<SimpleEntity> entities = session.byMultipleIds( SimpleEntity.class )
.with( CacheMode.NORMAL )
.enableSessionCheck( true )
@ -322,9 +337,13 @@ public class MultiLoadTest implements SessionFactoryProducer {
.multiLoad( ids( 3 ) );
assertEquals( 3, entities.size() );
assertNull( entities.get(1) );
assertNull( entities.get( 1 ) );
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?,?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 2 ) );
}
);
}
@ -347,19 +366,23 @@ public class MultiLoadTest implements SessionFactoryProducer {
.multiLoad( ids( 3 ) );
assertEquals( 3, entities.size() );
SimpleEntity deletedEntity = entities.get(1);
SimpleEntity deletedEntity = entities.get( 1 );
assertNotNull( deletedEntity );
final EntityEntry entry = ((SharedSessionContractImplementor) session).getPersistenceContext().getEntry( deletedEntity );
final EntityEntry entry = session.getPersistenceContext()
.getEntry( deletedEntity );
assertTrue( entry.getStatus() == Status.DELETED || entry.getStatus() == Status.GONE );
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?, ?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 2 ) );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-12944")
@FailureExpected( reason = "Caching/CacheMode supported not yet implemented" )
public void testUnorderedMultiLoadFrom2ndLevelCachePendingDelete(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
@ -378,7 +401,11 @@ public class MultiLoadTest implements SessionFactoryProducer {
assertTrue( entities.stream().anyMatch( Objects::isNull ) );
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?,?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 2 ) );
} );
}
@ -401,13 +428,19 @@ public class MultiLoadTest implements SessionFactoryProducer {
.multiLoad( ids( 3 ) );
assertEquals( 3, entities.size() );
SimpleEntity deletedEntity = entities.stream().filter( simpleEntity -> simpleEntity.getId().equals( 2 ) ).findAny().orElse( null );
SimpleEntity deletedEntity = entities.stream().filter( simpleEntity -> simpleEntity.getId()
.equals( 2 ) ).findAny().orElse( null );
assertNotNull( deletedEntity );
final EntityEntry entry = ((SharedSessionContractImplementor) session).getPersistenceContext().getEntry( deletedEntity );
final EntityEntry entry = ( (SharedSessionContractImplementor) session ).getPersistenceContext()
.getEntry( deletedEntity );
assertTrue( entry.getStatus() == Status.DELETED || entry.getStatus() == Status.GONE );
assertTrue( sqlStatementInterceptor.getSqlQueries().getFirst().endsWith( "id in (?, ?)" ) );
final int paramCount = StringHelper.countUnquoted(
sqlStatementInterceptor.getSqlQueries().getFirst(),
'?'
);
assertThat( paramCount, is( 2 ) );
} );
}
@ -437,7 +470,7 @@ public class MultiLoadTest implements SessionFactoryProducer {
public void testMultiLoadClearsBatchFetchQueue(SessionFactoryScope scope) {
final EntityKey entityKey = new EntityKey(
1,
scope.getSessionFactory().getEntityPersister( SimpleEntity.class.getName() )
scope.getSessionFactory().getMetamodel().entityPersister( SimpleEntity.class.getName() )
);
scope.inTransaction(

View File

@ -32,6 +32,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -284,7 +286,11 @@ public class BatchFetchNotFoundIgnoreDefaultStyleTest extends BaseCoreFunctional
sessionImplementor.getFactory().getMetamodel().entityPersister( Task.class );
final BatchFetchQueue batchFetchQueue =
sessionImplementor.getPersistenceContextInternal().getBatchFetchQueue();
assertEquals( expected, batchFetchQueue.containsEntityKey( new EntityKey( id, persister ) ) );
assertThat(
"Checking BatchFetchQueue for entry for Task#" + id,
batchFetchQueue.containsEntityKey( new EntityKey( id, persister ) ),
is( expected )
);
}
@Entity(name = "Employee")