diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/MapOperationTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/MapOperationTests.java index 35e762b262..1ddcf35136 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/MapOperationTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/MapOperationTests.java @@ -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> iterator = entity.getSortedBasicByBasic().entrySet().iterator(); + final Map.Entry first = iterator.next(); + final Map.Entry 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 diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/PluralAttributeMappingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/PluralAttributeMappingTests.java index 6f1f727d14..54e421ba9c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/PluralAttributeMappingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/collections/PluralAttributeMappingTests.java @@ -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() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/SelectClauseTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/SelectClauseTests.java index eeba1efa3b..f9731e1be4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/SelectClauseTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/SelectClauseTests.java @@ -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() diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/domain/gambit/EntityOfMaps.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/domain/gambit/EntityOfMaps.java index f5052ee2c2..6a885a44a2 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/domain/gambit/EntityOfMaps.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/domain/gambit/EntityOfMaps.java @@ -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 basicByBasic; + private SortedMap sortedBasicByBasic; + private Map basicByEnum; private Map basicByConvertedEnum; @@ -39,7 +45,9 @@ public class EntityOfMaps { private Map oneToManyByBasic; private Map basicByOneToMany; + private Map manyToManyByBasic; + private SortedMap sortedManyToManyByBasic; private Map componentByBasicOrdered; @@ -88,6 +96,25 @@ public class EntityOfMaps { basicByBasic.put( key, val ); } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // sortedBasicByBasic + + @ElementCollection + @SortNatural + public SortedMap getSortedBasicByBasic() { + return sortedBasicByBasic; + } + + public void setSortedBasicByBasic(SortedMap 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 getSortedManyToManyByBasic() { + return sortedManyToManyByBasic; + } + + public void setSortedManyToManyByBasic(SortedMap sortedManyToManyByBasic) { + this.sortedManyToManyByBasic = sortedManyToManyByBasic; + } + + public void addSortedManyToManyByComponent(String key, SimpleEntity value) { + if ( sortedManyToManyByBasic == null ) { + sortedManyToManyByBasic = new TreeMap<>(); + } + sortedManyToManyByBasic.put( key, value ); + } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // componentByBasicOrdered