HHH-12835 Fix an incorrect assertion in BatchFetchQueueHelper

This commit is contained in:
Guillaume Smet 2018-07-31 17:11:24 +02:00
parent d85831f543
commit bee200a84c
4 changed files with 204 additions and 1 deletions

View File

@ -54,11 +54,12 @@ public class BatchFetchQueueHelper {
}
LOG.debug( "Not all entities were loaded." );
Set<Serializable> idSet = new HashSet<>( Arrays.asList( ids ) );
int originalIdSetSize = idSet.size();
for ( Object result : results ) {
// All results should be in the PersistenceContext
idSet.remove( session.getContextEntityIdentifier( result ) );
}
assert idSet.size() == ids.length - results.size();
assert idSet.size() == originalIdSetSize - results.size();
if ( LOG.isDebugEnabled() ) {
LOG.debug( "Entities of type [" + persister.getEntityName() + "] not found; IDs: " + idSet );
}

View File

@ -0,0 +1,72 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.batchfetch;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "city")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id")
private Country country;
public City() {
}
public City(String name, Country country) {
super();
this.name = name;
this.country = country;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Override
public String toString() {
return name + " (" + ( country == null ? "?" : country.getName() ) + ")";
}
}

View File

@ -0,0 +1,66 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.batchfetch;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "country")
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "country")
private List<City> cities;
public Country() {
}
public Country(String name) {
super();
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<City> getCities() {
return cities;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,64 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.batchfetch;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import java.util.stream.IntStream;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public class PaddedBatchFetchTestCase extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{ Country.class, City.class };
}
@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() );
configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() );
configuration.setProperty( AvailableSettings.BATCH_FETCH_STYLE, BatchFetchStyle.PADDED.name() );
configuration.setProperty( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "15" );
}
@Test
@TestForIssue(jiraKey = "HHH-12835")
public void paddedBatchFetchTest() throws Exception {
doInHibernate( this::sessionFactory, session -> {
// Having DEFAULT_BATCH_FETCH_SIZE=15
// results in batchSizes = [15, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
// Let's create 11 countries so batch size 15 will be used with padded values,
// this causes to have to remove 4 elements from list
int numberOfCountries = 11;
IntStream.range( 0, numberOfCountries ).forEach( i -> {
Country c = new Country( "Country " + i );
session.save( c );
session.save( new City( "City " + i, c ) );
} );
} );
doInHibernate( this::sessionFactory, session -> {
List<City> allCities = session.createQuery( "from City", City.class ).list();
// this triggers countries to be fetched in batch
assertNotNull( allCities.get( 0 ).getCountry().getName() );
} );
}
}