HH-13720 Test mapping model support for plural attributes - sorted map

This commit is contained in:
Fabio Massimo Ercoli 2019-12-23 12:38:01 +01:00 committed by Steve Ebersole
parent 8defa9619e
commit ccc3c58367
4 changed files with 95 additions and 2 deletions

View File

@ -6,9 +6,13 @@
*/
package org.hibernate.orm.test.metamodel.mapping.collections;
import java.util.Iterator;
import java.util.Map;
import org.hibernate.Hibernate;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.testing.hamcrest.InitializationCheckMatcher;
import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.gambit.EntityOfMaps;
import org.hibernate.testing.orm.domain.gambit.EnumValue;
@ -22,11 +26,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.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Steve Ebersole
* @author Fabio Massimo Ercoli
*/
@SuppressWarnings("WeakerAccess")
@DomainModel( standardModels = StandardDomainModel.GAMBIT )
@ -41,6 +47,9 @@ public class MapOperationTests {
entityContainingMaps.addBasicByBasic( "someKey", "someValue" );
entityContainingMaps.addBasicByBasic( "anotherKey", "anotherValue" );
entityContainingMaps.addSortedBasicByBasic( "key1", "value1" );
entityContainingMaps.addSortedBasicByBasic( "key2", "value2" );
entityContainingMaps.addBasicByEnum( EnumValue.ONE, "one" );
entityContainingMaps.addBasicByEnum( EnumValue.TWO, "two" );
@ -104,6 +113,30 @@ public class MapOperationTests {
createData( scope );
}
@Test
public void testSortedMapAccess(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final EntityOfMaps entity = session.get( EntityOfMaps.class, 1 );
assertThat( entity.getSortedBasicByBasic(), InitializationCheckMatcher.isNotInitialized() );
// trigger the init
Hibernate.initialize( entity.getSortedBasicByBasic() );
assertThat( entity.getSortedBasicByBasic(), InitializationCheckMatcher.isInitialized() );
assertThat( entity.getSortedBasicByBasic().size(), is( 2 ) );
assertThat( entity.getBasicByEnum(), InitializationCheckMatcher.isNotInitialized() );
final Iterator<Map.Entry<String, String>> iterator = entity.getSortedBasicByBasic().entrySet().iterator();
final Map.Entry<String, String> first = iterator.next();
final Map.Entry<String, String> second = iterator.next();
assertThat( first.getKey(), is( "key1" ) );
assertThat( first.getValue(), is( "value1" ) );
assertThat( second.getKey(), is( "key2" ) );
assertThat( second.getValue(), is( "value2" ) );
}
);
}
@Test
public void testOrderedMap(SessionFactoryScope scope) {
// atm we can only check the fragment translation

View File

@ -27,6 +27,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Steve Ebersole
* @author Fabio Massimo Ercoli
*/
@DomainModel( standardModels = StandardDomainModel.GAMBIT )
@ServiceRegistry
@ -101,13 +102,18 @@ public class PluralAttributeMappingTests {
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor( EntityOfMaps.class );
// 8 for now, until entity-valued map keys is supported
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 10 ) );
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 12 ) );
final PluralAttributeMapping basicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping( "basicByBasic" );
assertThat( basicByBasic, notNullValue() );
assertThat( basicByBasic.getKeyDescriptor(), notNullValue() );
assertThat( basicByBasic.getElementDescriptor(), notNullValue() );
final PluralAttributeMapping sortedBasicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping( "sortedBasicByBasic" );
assertThat( sortedBasicByBasic, notNullValue() );
assertThat( sortedBasicByBasic.getKeyDescriptor(), notNullValue() );
assertThat( sortedBasicByBasic.getElementDescriptor(), notNullValue() );
final PluralAttributeMapping basicByEnum = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping( "basicByEnum" );
assertThat( basicByEnum, notNullValue() );
assertThat( basicByEnum.getKeyDescriptor(), notNullValue() );
@ -143,6 +149,11 @@ public class PluralAttributeMappingTests {
assertThat( manyToManyByBasic.getKeyDescriptor(), notNullValue() );
assertThat( manyToManyByBasic.getElementDescriptor(), notNullValue() );
final PluralAttributeMapping sortedManyToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping( "sortedManyToManyByBasic" );
assertThat( sortedManyToManyByBasic, notNullValue() );
assertThat( sortedManyToManyByBasic.getKeyDescriptor(), notNullValue() );
assertThat( sortedManyToManyByBasic.getElementDescriptor(), notNullValue() );
final PluralAttributeMapping componentByBasicOrdered = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping( "componentByBasicOrdered" );
assertThat( componentByBasicOrdered, notNullValue() );
assertThat( componentByBasicOrdered.getKeyDescriptor(), notNullValue() );

View File

@ -285,8 +285,11 @@ public class SelectClauseTests extends BaseSqmUnitTest {
@Test
public void testMapEntryFunction() {
SqmSelectStatement statement = interpretSelect( "select entry(m) from EntityOfMaps e join e.manyToManyByBasic m" );
testMapEntryFunctionAssertions( interpretSelect( "select entry(m) from EntityOfMaps e join e.manyToManyByBasic m" ) );
testMapEntryFunctionAssertions( interpretSelect( "select entry(m) from EntityOfMaps e join e.sortedManyToManyByBasic m" ) );
}
private void testMapEntryFunctionAssertions(SqmSelectStatement statement) {
assertEquals( 1, statement.getQuerySpec().getSelectClause().getSelections().size() );
final SqmMapEntryReference mapEntryPath = (SqmMapEntryReference) statement.getQuerySpec()

View File

@ -9,6 +9,8 @@ package org.hibernate.testing.orm.domain.gambit;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.persistence.Convert;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
@ -20,9 +22,11 @@ import javax.persistence.MapKeyEnumerated;
import javax.persistence.OneToMany;
import org.hibernate.annotations.OrderBy;
import org.hibernate.annotations.SortNatural;
/**
* @author Steve Ebersole
* @author Fabio Massimo Ercoli
*/
@SuppressWarnings("unused")
@Entity
@ -31,6 +35,8 @@ public class EntityOfMaps {
private String name;
private Map<String,String> basicByBasic;
private SortedMap<String, String> sortedBasicByBasic;
private Map<EnumValue,String> basicByEnum;
private Map<EnumValue,String> basicByConvertedEnum;
@ -39,7 +45,9 @@ public class EntityOfMaps {
private Map<String, SimpleEntity> oneToManyByBasic;
private Map<SimpleEntity,String> basicByOneToMany;
private Map<String, SimpleEntity> manyToManyByBasic;
private SortedMap<String, SimpleEntity> sortedManyToManyByBasic;
private Map<String, SimpleComponent> componentByBasicOrdered;
@ -88,6 +96,25 @@ public class EntityOfMaps {
basicByBasic.put( key, val );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sortedBasicByBasic
@ElementCollection
@SortNatural
public SortedMap<String, String> getSortedBasicByBasic() {
return sortedBasicByBasic;
}
public void setSortedBasicByBasic(SortedMap<String, String> sortedBasicByBasic) {
this.sortedBasicByBasic = sortedBasicByBasic;
}
public void addSortedBasicByBasic(String key, String val) {
if ( sortedBasicByBasic == null ) {
sortedBasicByBasic = new TreeMap<>();
}
sortedBasicByBasic.put( key, val );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// basicByEnum
@ -231,6 +258,25 @@ public class EntityOfMaps {
manyToManyByBasic.put( key, value );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sortedManyToManyByBasic
@ManyToMany
@SortNatural
public SortedMap<String, SimpleEntity> getSortedManyToManyByBasic() {
return sortedManyToManyByBasic;
}
public void setSortedManyToManyByBasic(SortedMap<String, SimpleEntity> sortedManyToManyByBasic) {
this.sortedManyToManyByBasic = sortedManyToManyByBasic;
}
public void addSortedManyToManyByComponent(String key, SimpleEntity value) {
if ( sortedManyToManyByBasic == null ) {
sortedManyToManyByBasic = new TreeMap<>();
}
sortedManyToManyByBasic.put( key, value );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// componentByBasicOrdered