HHH-11147 - Add back assertions to BatchFetchProxyTest checking that batches are

initialized as expected. Also add a test that ensures that entities
            loaded from a batch can be modified.
This commit is contained in:
Gail Badner 2019-06-27 15:47:25 -07:00 committed by Sanne Grinovero
parent 1b4eea59b6
commit a49b7902cc
1 changed files with 85 additions and 10 deletions

View File

@ -15,7 +15,6 @@ import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
@ -33,9 +32,10 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
@ -49,11 +49,43 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase {
private static int NUMBER_OF_ENTITIES = 20;
@Test
@TestForIssue(jiraKey = "HHH-11147")
public void testBatchAssociationFetch() {
inTransaction(
session -> {
final Statistics statistics = sessionFactory().getStatistics();
statistics.clear();
List<Employee> employees = session.createQuery( "from Employee", Employee.class ).getResultList();
assertEquals( 1, statistics.getPrepareStatementCount() );
assertEquals( NUMBER_OF_ENTITIES, employees.size() );
for ( int i = 0; i < employees.size(); i++ ) {
final Employer employer = employees.get( i ).employer;
if ( i % 10 == 0 ) {
assertFalse( Hibernate.isInitialized( employer ) );
Hibernate.initialize( employer );
}
else {
assertTrue( Hibernate.isInitialized( employer ) );
}
assertEquals( "Employer #" + employer.id, employer.name );
}
// assert that all 20 Employee and all 20 Employers have been loaded
assertThat( statistics.getEntityLoadCount(), is( 40L ) );
// but assert that it only took 3 queries (the initial plus the 2 batch fetches)
assertThat( statistics.getPrepareStatementCount(), is( 3L ) );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-11147")
public void testBatchAssociation() {
doInHibernate(
this::sessionFactory, session -> {
inTransaction(
session -> {
final Statistics statistics = sessionFactory().getStatistics();
statistics.clear();
List<Employee> employees = session.createQuery( "from Employee", Employee.class ).getResultList();
@ -81,8 +113,8 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-11147")
public void testBatchEntityLoad() {
doInHibernate(
this::sessionFactory, session -> {
inTransaction(
session -> {
final Statistics statistics = sessionFactory().getStatistics();
statistics.clear();
@ -111,6 +143,48 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase {
}
@Test
@TestForIssue(jiraKey = "HHH-11147")
public void testBatchEntityLoadThenModify() {
inTransaction(
session -> {
final Statistics statistics = sessionFactory().getStatistics();
statistics.clear();
List<Employer> employers = new ArrayList<>();
for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) {
employers.add( session.load( Employer.class, i + 1) );
}
assertEquals( 0, statistics.getPrepareStatementCount() );
for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) {
final Employer employer = employers.get( i );
if ( i % 10 == 0 ) {
assertFalse( Hibernate.isInitialized( employer ) );
Hibernate.initialize( employer );
}
else {
assertTrue( Hibernate.isInitialized( employer ) );
}
assertEquals( "Employer #" + employer.id, employer.name );
employer.name = employer.name + " new";
}
assertEquals( 2, statistics.getPrepareStatementCount() );
}
);
inTransaction(
session -> {
for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) {
final Employer employer = session.get( Employer.class, i + 1 );
assertEquals( "Employer #" + employer.id + " new", employer.name );
}
}
);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
@ -121,8 +195,8 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase {
@Before
public void setUpData() {
doInHibernate(
this::sessionFactory, session -> {
inTransaction(
session -> {
for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) {
final Employee employee = new Employee();
employee.id = i + 1;
@ -139,8 +213,8 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase {
@After
public void cleanupDate() {
doInHibernate(
this::sessionFactory, session -> {
inTransaction(
session -> {
session.createQuery( "delete from Employee" ).executeUpdate();
session.createQuery( "delete from Employer" ).executeUpdate();
}
@ -153,6 +227,7 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase {
ssrb.applySetting( AvailableSettings.ALLOW_ENHANCEMENT_AS_PROXY, "true" );
ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" );
ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" );
ssrb.applySetting( AvailableSettings.SHOW_SQL, true );
}
@Override