wrap up verification for both @SortedSet and @SortedMap
This commit is contained in:
parent
cce8f823e7
commit
7ee45f68ee
|
@ -409,55 +409,55 @@ public abstract class AbstractSqmFrom<O,T> extends AbstractSqmPath<T> implements
|
|||
SqmPathSource<A> joinedPathSource,
|
||||
SqmJoinType joinType,
|
||||
boolean fetched) {
|
||||
final SqmAttributeJoin sqmJoin;
|
||||
if ( joinedPathSource instanceof SingularPersistentAttribute ) {
|
||||
return buildSingularJoin(
|
||||
sqmJoin = buildSingularJoin(
|
||||
(SingularPersistentAttribute<T,A>) joinedPathSource,
|
||||
joinType,
|
||||
fetched
|
||||
);
|
||||
}
|
||||
|
||||
if ( joinedPathSource instanceof BagPersistentAttribute ) {
|
||||
return buildBagJoin(
|
||||
else if ( joinedPathSource instanceof BagPersistentAttribute ) {
|
||||
sqmJoin = buildBagJoin(
|
||||
(BagPersistentAttribute) joinedPathSource,
|
||||
joinType,
|
||||
fetched
|
||||
);
|
||||
}
|
||||
|
||||
if ( joinedPathSource instanceof ListPersistentAttribute ) {
|
||||
return buildListJoin(
|
||||
else if ( joinedPathSource instanceof ListPersistentAttribute ) {
|
||||
sqmJoin = buildListJoin(
|
||||
(ListPersistentAttribute) joinedPathSource,
|
||||
joinType,
|
||||
fetched
|
||||
);
|
||||
}
|
||||
|
||||
if ( joinedPathSource instanceof MapPersistentAttribute ) {
|
||||
return buildMapJoin(
|
||||
else if ( joinedPathSource instanceof MapPersistentAttribute ) {
|
||||
sqmJoin = buildMapJoin(
|
||||
(MapPersistentAttribute) joinedPathSource,
|
||||
joinType,
|
||||
fetched
|
||||
);
|
||||
}
|
||||
|
||||
if ( joinedPathSource instanceof SetPersistentAttribute ) {
|
||||
return buildSetJoin(
|
||||
else if ( joinedPathSource instanceof SetPersistentAttribute ) {
|
||||
sqmJoin = buildSetJoin(
|
||||
(SetPersistentAttribute) joinedPathSource,
|
||||
joinType,
|
||||
fetched
|
||||
);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
Locale.ROOT,
|
||||
"Passed attribute [%s] did not correspond to a joinable reference [%s] relative to %s",
|
||||
joinedPathSource.getPathName(),
|
||||
joinedPathSource,
|
||||
getNavigablePath().getFullPath()
|
||||
)
|
||||
);
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
Locale.ROOT,
|
||||
"Passed attribute [%s] did not correspond to a joinable reference [%s] relative to %s",
|
||||
joinedPathSource.getPathName(),
|
||||
joinedPathSource,
|
||||
getNavigablePath().getFullPath()
|
||||
)
|
||||
);
|
||||
}
|
||||
addSqmJoin( sqmJoin );
|
||||
return sqmJoin;
|
||||
}
|
||||
|
||||
private <A> SqmSingularJoin<T,A> buildSingularJoin(
|
||||
|
|
|
@ -14,19 +14,18 @@ import org.hibernate.testing.orm.domain.StandardDomainModel;
|
|||
import org.hibernate.testing.orm.domain.gambit.EntityOfLists;
|
||||
import org.hibernate.testing.orm.domain.gambit.EnumValue;
|
||||
import org.hibernate.testing.orm.domain.gambit.SimpleComponent;
|
||||
import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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;
|
||||
import static org.hibernate.testing.hamcrest.CollectionMatchers.hasSize;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
|
@ -102,7 +101,7 @@ public class ListOperationTests {
|
|||
assertThat( result.getListOfBasics(), notNullValue() );
|
||||
assertThat( Hibernate.isInitialized( result.getListOfBasics() ), is( true ) );
|
||||
assertTrue( session.getPersistenceContext().containsCollection( (PersistentCollection) result.getListOfBasics() ) );
|
||||
assertThat( result.getListOfBasics().size(), is( 3 ) );
|
||||
assertThat( result.getListOfBasics(), hasSize( 3 ) );
|
||||
|
||||
assertThat( result.getListOfConvertedEnums(), notNullValue() );
|
||||
assertThat( Hibernate.isInitialized( result.getListOfConvertedEnums() ), is( false ) );
|
||||
|
|
|
@ -50,6 +50,9 @@ public class MapOperationTests {
|
|||
entityContainingMaps.addSortedBasicByBasic( "key1", "value1" );
|
||||
entityContainingMaps.addSortedBasicByBasic( "key2", "value2" );
|
||||
|
||||
entityContainingMaps.addSortedBasicByBasicWithComparator( "kEy1", "value1" );
|
||||
entityContainingMaps.addSortedBasicByBasicWithComparator( "KeY2", "value2" );
|
||||
|
||||
entityContainingMaps.addBasicByEnum( EnumValue.ONE, "one" );
|
||||
entityContainingMaps.addBasicByEnum( EnumValue.TWO, "two" );
|
||||
|
||||
|
@ -137,6 +140,30 @@ public class MapOperationTests {
|
|||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortedMapWithComparatorAccess(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final EntityOfMaps entity = session.get( EntityOfMaps.class, 1 );
|
||||
assertThat( entity.getSortedBasicByBasicWithComparator(), InitializationCheckMatcher.isNotInitialized() );
|
||||
|
||||
// trigger the init
|
||||
Hibernate.initialize( entity.getSortedBasicByBasicWithComparator() );
|
||||
assertThat( entity.getSortedBasicByBasicWithComparator(), InitializationCheckMatcher.isInitialized() );
|
||||
assertThat( entity.getSortedBasicByBasicWithComparator().size(), is( 2 ) );
|
||||
assertThat( entity.getBasicByEnum(), InitializationCheckMatcher.isNotInitialized() );
|
||||
|
||||
final Iterator<Map.Entry<String, String>> iterator = entity.getSortedBasicByBasicWithComparator().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
|
||||
|
|
|
@ -78,7 +78,7 @@ public class PluralAttributeMappingTests {
|
|||
final MappingMetamodel domainModel = scope.getSessionFactory().getDomainModel();
|
||||
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor( EntityOfSets.class );
|
||||
|
||||
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 10 ) );
|
||||
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 11 ) );
|
||||
|
||||
final AttributeMapping setOfBasics = containerEntityDescriptor.findAttributeMapping( "setOfBasics" );
|
||||
assertThat( setOfBasics, notNullValue() );
|
||||
|
@ -113,8 +113,8 @@ public class PluralAttributeMappingTests {
|
|||
final MappingMetamodel domainModel = scope.getSessionFactory().getDomainModel();
|
||||
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor( EntityOfMaps.class );
|
||||
|
||||
// 8 for now, until entity-valued map keys is supported
|
||||
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 12 ) );
|
||||
// 14 for now, until entity-valued map keys is supported
|
||||
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 14 ) );
|
||||
|
||||
final PluralAttributeMapping basicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping( "basicByBasic" );
|
||||
assertThat( basicByBasic, notNullValue() );
|
||||
|
|
|
@ -6,19 +6,14 @@
|
|||
*/
|
||||
package org.hibernate.orm.test.metamodel.mapping.collections;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.persister.collection.CollectionPersister;
|
||||
|
||||
import org.hibernate.testing.hamcrest.CollectionMatchers;
|
||||
import org.hibernate.testing.hamcrest.InitializationCheckMatcher;
|
||||
import org.hibernate.testing.orm.domain.StandardDomainModel;
|
||||
import org.hibernate.testing.orm.domain.gambit.EntityOfSets;
|
||||
import org.hibernate.testing.orm.domain.gambit.EnumValue;
|
||||
import org.hibernate.testing.orm.domain.gambit.SimpleComponent;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.DomainModelScope;
|
||||
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
|
@ -26,14 +21,17 @@ 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 org.hamcrest.collection.IsIterableContainingInOrder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hibernate.testing.hamcrest.CollectionMatchers.hasSize;
|
||||
import static org.hibernate.testing.hamcrest.InitializationCheckMatcher.isInitialized;
|
||||
import static org.hibernate.testing.hamcrest.InitializationCheckMatcher.isNotInitialized;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@DomainModel( standardModels = StandardDomainModel.GAMBIT )
|
||||
@ServiceRegistry
|
||||
@SessionFactory
|
||||
|
@ -47,8 +45,17 @@ public class SetOperationTests {
|
|||
entity.addBasic( "another value" );
|
||||
|
||||
entity.addSortedBasic( "def" );
|
||||
entity.addSortedBasic( "cde" );
|
||||
entity.addSortedBasic( "bcd" );
|
||||
entity.addSortedBasic( "efg" );
|
||||
entity.addSortedBasic( "abc" );
|
||||
|
||||
entity.addSortedBasicWithComparator( "DeF" );
|
||||
entity.addSortedBasicWithComparator( "cDe" );
|
||||
entity.addSortedBasicWithComparator( "bcD" );
|
||||
entity.addSortedBasicWithComparator( "Efg" );
|
||||
entity.addSortedBasicWithComparator( "aBC" );
|
||||
|
||||
entity.addEnum( EnumValue.ONE );
|
||||
entity.addEnum( EnumValue.TWO );
|
||||
|
||||
|
@ -66,9 +73,7 @@ public class SetOperationTests {
|
|||
@AfterEach
|
||||
public void dropData(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.createQuery( "delete from EntityOfSets where name is not null" ).executeUpdate();
|
||||
}
|
||||
session -> session.createQuery( "delete from EntityOfSets where name is not null" ).executeUpdate()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -77,8 +82,8 @@ public class SetOperationTests {
|
|||
scope.inTransaction(
|
||||
session -> {
|
||||
final EntityOfSets entity = session.load( EntityOfSets.class, 1 );
|
||||
assertThat( entity, is( notNullValue() ) );
|
||||
assertThat( entity, InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity, notNullValue() );
|
||||
assertThat( entity, isNotInitialized() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -88,9 +93,9 @@ public class SetOperationTests {
|
|||
scope.inTransaction(
|
||||
session -> {
|
||||
final EntityOfSets entity = session.get( EntityOfSets.class, 1 );
|
||||
assertThat( entity, is( notNullValue() ) );
|
||||
assertThat( entity, InitializationCheckMatcher.isInitialized() );
|
||||
assertThat( entity.getSetOfBasics(), InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity, notNullValue() );
|
||||
assertThat( entity, isInitialized() );
|
||||
assertThat( entity.getSetOfBasics(), isNotInitialized() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -104,7 +109,7 @@ public class SetOperationTests {
|
|||
EntityOfSets.class
|
||||
).getSingleResult();
|
||||
|
||||
assertThat( entity.getSetOfBasics(), InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity.getSetOfBasics(), isNotInitialized() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -118,9 +123,8 @@ public class SetOperationTests {
|
|||
EntityOfSets.class
|
||||
).getSingleResult();
|
||||
|
||||
assertThat( entity.getSetOfBasics(), InitializationCheckMatcher.isInitialized() );
|
||||
|
||||
assert entity.getSetOfBasics().size() == 2;
|
||||
assertThat( entity.getSetOfBasics(), isInitialized() );
|
||||
assertThat( entity.getSetOfBasics(), hasSize( 2 ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -143,13 +147,13 @@ public class SetOperationTests {
|
|||
scope.inTransaction(
|
||||
session -> {
|
||||
final EntityOfSets entity = session.get( EntityOfSets.class, 1 );
|
||||
assertThat( entity.getSetOfBasics(), InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity.getSetOfBasics(), isNotInitialized() );
|
||||
|
||||
// trigger the init
|
||||
assertThat( entity.getSetOfBasics().size(), is( 2 ) );
|
||||
assertThat( entity.getSetOfBasics(), hasSize( 2 ) );
|
||||
|
||||
assertThat( entity.getSetOfBasics(), InitializationCheckMatcher.isInitialized() );
|
||||
assertThat( entity.getSetOfEnums(), InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity.getSetOfBasics(), isInitialized() );
|
||||
assertThat( entity.getSetOfEnums(), isNotInitialized() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -159,19 +163,45 @@ public class SetOperationTests {
|
|||
scope.inTransaction(
|
||||
session -> {
|
||||
final EntityOfSets entity = session.get( EntityOfSets.class, 1 );
|
||||
assertThat( entity.getSortedSetOfBasics(), InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity.getSortedSetOfBasics(), isNotInitialized() );
|
||||
|
||||
// trigger the init
|
||||
Hibernate.initialize( entity.getSortedSetOfBasics() );
|
||||
assertThat( entity.getSortedSetOfBasics(), InitializationCheckMatcher.isInitialized() );
|
||||
assertThat( entity.getSortedSetOfBasics().size(), is( 2 ) );
|
||||
assertThat( entity.getSetOfEnums(), InitializationCheckMatcher.isNotInitialized() );
|
||||
assertThat( entity.getSortedSetOfBasics(), isInitialized() );
|
||||
assertThat( entity.getSortedSetOfBasics(), hasSize( 5 ) );
|
||||
assertThat( entity.getSetOfEnums(), isNotInitialized() );
|
||||
|
||||
final Iterator<String> iterator = entity.getSortedSetOfBasics().iterator();
|
||||
final String first = iterator.next();
|
||||
final String second = iterator.next();
|
||||
assertThat( first, is( "abc" ) );
|
||||
assertThat( second, is( "def" ) );
|
||||
assertThat( entity.getSortedSetOfBasics(), IsIterableContainingInOrder.contains(
|
||||
"abc",
|
||||
"bcd",
|
||||
"cde",
|
||||
"def",
|
||||
"efg"
|
||||
) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortedSetWithComparatorAccess(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final EntityOfSets entity = session.get( EntityOfSets.class, 1 );
|
||||
assertThat( entity.getSortedSetOfBasicsWithComparator(), isNotInitialized() );
|
||||
|
||||
// trigger the init
|
||||
Hibernate.initialize( entity.getSortedSetOfBasicsWithComparator() );
|
||||
assertThat( entity.getSortedSetOfBasicsWithComparator(), isInitialized() );
|
||||
assertThat( entity.getSortedSetOfBasicsWithComparator(), hasSize( 5 ) );
|
||||
assertThat( entity.getSetOfEnums(), isNotInitialized() );
|
||||
|
||||
assertThat( entity.getSortedSetOfBasicsWithComparator(), IsIterableContainingInOrder.contains(
|
||||
"aBC",
|
||||
"bcD",
|
||||
"cDe",
|
||||
"DeF",
|
||||
"Efg"
|
||||
) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,278 @@
|
|||
/*
|
||||
* 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.orm.test.sorted;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
import org.hibernate.mapping.Collection;
|
||||
import org.hibernate.mapping.PersistentClass;
|
||||
import org.hibernate.mapping.Property;
|
||||
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.hamcrest.collection.IsIterableContainingInOrder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hibernate.testing.hamcrest.CollectionMatchers.hasSize;
|
||||
import static org.hibernate.testing.hamcrest.InitializationCheckMatcher.isInitialized;
|
||||
import static org.hibernate.testing.hamcrest.InitializationCheckMatcher.isNotInitialized;
|
||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
* @author Brett Meyer
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
@DomainModel( annotatedClasses = SortTest.Search.class )
|
||||
@SessionFactory
|
||||
@SuppressWarnings( "unused" )
|
||||
public class SortTest {
|
||||
|
||||
private String[] searchResultsBeforeSorting = {
|
||||
"jboss.com",
|
||||
"hibernate.org",
|
||||
"HiA"
|
||||
};
|
||||
|
||||
private Map<String, String> tokensBeforeSorting = new HashMap<String, String>()
|
||||
{
|
||||
{
|
||||
put( "jboss", "jboss" );
|
||||
put( "hibernate", "hibernate" );
|
||||
put( "HiA", "hia");
|
||||
}
|
||||
};
|
||||
|
||||
@BeforeEach
|
||||
void setUp(SessionFactoryScope scope) {
|
||||
|
||||
// ensure query plan cache won't interfere
|
||||
scope.getSessionFactory().getQueryEngine().getInterpretationCache().close();
|
||||
|
||||
final Search search = new Search( "Hibernate" );
|
||||
search.searchResults.addAll( Arrays.asList( searchResultsBeforeSorting ) );
|
||||
search.searchResultsCaseInsensitive.addAll( Arrays.asList( searchResultsBeforeSorting ) );
|
||||
search.tokens.putAll( tokensBeforeSorting );
|
||||
search.tokensCaseInsensitive.putAll( tokensBeforeSorting );
|
||||
scope.inTransaction(
|
||||
session -> session.persist( search )
|
||||
);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> session.createQuery( "delete from Search" ).executeUpdate()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSortedSetDefinitionInHbmXml(SessionFactoryScope scope) {
|
||||
final PersistentClass entityMapping = scope.getMetadataImplementor().getEntityBinding( Search.class.getName() );
|
||||
|
||||
final Property sortedSetProperty = entityMapping.getProperty( "searchResults" );
|
||||
final Collection sortedSetMapping = assertTyping( Collection.class, sortedSetProperty.getValue() );
|
||||
assertTrue( "SortedSet mapping not interpreted as sortable", sortedSetMapping.isSorted() );
|
||||
assertNull( sortedSetMapping.getComparator() );
|
||||
|
||||
final Property sortedMapProperty = entityMapping.getProperty( "tokens" );
|
||||
final Collection sortedMapMapping = assertTyping( Collection.class, sortedMapProperty.getValue() );
|
||||
assertTrue( "SortedMap mapping not interpreted as sortable", sortedMapMapping.isSorted() );
|
||||
assertNull( sortedSetMapping.getComparator() );
|
||||
|
||||
final Property sortedSetCaseInsensitiveProperty = entityMapping.getProperty( "searchResultsCaseInsensitive" );
|
||||
final Collection sortedSetCaseInsensitiveMapping = assertTyping(
|
||||
Collection.class,
|
||||
sortedSetCaseInsensitiveProperty.getValue()
|
||||
);
|
||||
assertTrue( "SortedSet mapping not interpreted as sortable", sortedSetCaseInsensitiveMapping.isSorted() );
|
||||
assertTyping( StringCaseInsensitiveComparator.class, sortedSetCaseInsensitiveMapping.getComparator() );
|
||||
|
||||
final Property sortedMapCaseInsensitiveProperty = entityMapping.getProperty( "tokensCaseInsensitive" );
|
||||
final Collection sortedMapCaseInsensitiveMapping = assertTyping(
|
||||
Collection.class,
|
||||
sortedMapCaseInsensitiveProperty.getValue()
|
||||
);
|
||||
assertTrue( "SortedMap mapping not interpreted as sortable", sortedMapCaseInsensitiveMapping.isSorted() );
|
||||
assertTyping( StringCaseInsensitiveComparator.class, sortedMapCaseInsensitiveMapping.getComparator() );
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSortByCriteria(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Search> criteria = criteriaBuilder.createQuery( Search.class );
|
||||
final Root<Search> root = criteria.from( Search.class );
|
||||
criteria.select( root );
|
||||
|
||||
final Search search = session.createQuery( criteria ).uniqueResult();
|
||||
|
||||
assertThat( search.searchResults, isNotInitialized() );
|
||||
assertSearchResultsOrderedNaturally( search.searchResults );
|
||||
|
||||
assertThat( search.searchResultsCaseInsensitive, isNotInitialized() );
|
||||
assertSearchResultsOrderedCaseInsensitively( search.searchResultsCaseInsensitive );
|
||||
|
||||
assertThat( search.tokens, isNotInitialized() );
|
||||
assertTokensOrderedNaturally( search.tokens );
|
||||
|
||||
assertThat( search.tokensCaseInsensitive, isNotInitialized() );
|
||||
assertTokensOrderedCaseInsensitively( search.tokensCaseInsensitive );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSortByCriteriaWithFetchLoad(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Search> criteria = criteriaBuilder.createQuery( Search.class );
|
||||
final Root<Search> root = criteria.from( Search.class );
|
||||
root.fetch( "searchResults", JoinType.LEFT );
|
||||
root.fetch( "searchResultsCaseInsensitive", JoinType.LEFT );
|
||||
root.fetch( "tokens", JoinType.LEFT );
|
||||
root.fetch( "tokensCaseInsensitive", JoinType.LEFT );
|
||||
criteria.select( root );
|
||||
|
||||
final Search search = session.createQuery( criteria ).uniqueResult();
|
||||
|
||||
assertThat( search.searchResults, isInitialized() );
|
||||
assertSearchResultsOrderedNaturally( search.searchResults );
|
||||
|
||||
assertThat( search.searchResultsCaseInsensitive, isInitialized() );
|
||||
assertSearchResultsOrderedCaseInsensitively( search.searchResultsCaseInsensitive );
|
||||
|
||||
assertThat( search.tokens, isInitialized() );
|
||||
assertTokensOrderedNaturally( search.tokens );
|
||||
|
||||
assertThat( search.tokensCaseInsensitive, isInitialized() );
|
||||
assertTokensOrderedCaseInsensitively( search.tokensCaseInsensitive );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSortByHQL(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final Search search = (Search) session.createQuery(
|
||||
"select s from Search s " +
|
||||
" join fetch s.searchResults " +
|
||||
" join fetch s.searchResultsCaseInsensitive " +
|
||||
" join fetch s.tokens " +
|
||||
" join fetch s.tokensCaseInsensitive " )
|
||||
.uniqueResult();
|
||||
|
||||
assertThat( search.searchResults, isInitialized() );
|
||||
assertSearchResultsOrderedNaturally( search.searchResults );
|
||||
|
||||
assertThat( search.searchResultsCaseInsensitive, isInitialized() );
|
||||
assertSearchResultsOrderedCaseInsensitively( search.searchResultsCaseInsensitive );
|
||||
|
||||
assertThat( search.tokens, isInitialized() );
|
||||
assertTokensOrderedNaturally( search.tokens );
|
||||
|
||||
assertThat( search.tokensCaseInsensitive, isInitialized() );
|
||||
assertTokensOrderedCaseInsensitively( search.tokensCaseInsensitive );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void assertSearchResultsOrderedNaturally(SortedSet<String> orderedSearchResults) {
|
||||
assertThat( orderedSearchResults, IsIterableContainingInOrder.contains(
|
||||
Arrays.stream( searchResultsBeforeSorting ).sorted().toArray()
|
||||
) );
|
||||
}
|
||||
|
||||
private void assertSearchResultsOrderedCaseInsensitively(SortedSet<String> orderedSearchResults) {
|
||||
assertThat( orderedSearchResults, IsIterableContainingInOrder.contains(
|
||||
Arrays.stream( searchResultsBeforeSorting ).sorted( String.CASE_INSENSITIVE_ORDER).toArray()
|
||||
) );
|
||||
}
|
||||
|
||||
private void assertTokensOrderedNaturally(SortedMap<String, String> orderedTokens) {
|
||||
assertTokensOrdered( orderedTokens, new TreeMap<>( tokensBeforeSorting ) );
|
||||
}
|
||||
|
||||
private void assertTokensOrderedCaseInsensitively(SortedMap<String, String> orderedTokens) {
|
||||
final TreeMap<String, String> expectedOrderedTokens = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
expectedOrderedTokens.putAll( tokensBeforeSorting );
|
||||
assertTokensOrdered( orderedTokens, expectedOrderedTokens );
|
||||
}
|
||||
|
||||
private void assertTokensOrdered(SortedMap<String, String> actualOrderedTokens, TreeMap<String, String> expectedOrderedTokens) {
|
||||
assertThat( actualOrderedTokens.entrySet(), hasSize( expectedOrderedTokens.entrySet().size() ) );
|
||||
final Iterator<Map.Entry<String, String>> actualIterator = actualOrderedTokens.entrySet().iterator();
|
||||
final Iterator<Map.Entry<String, String>> expectedIterator = expectedOrderedTokens.entrySet().iterator();
|
||||
while ( actualIterator.hasNext() ) {
|
||||
Map.Entry<String, String> actualEntry = actualIterator.next();
|
||||
Map.Entry<String, String> expectedEntry = expectedIterator.next();
|
||||
assertThat( actualEntry.getKey(), is( expectedEntry.getKey() ) );
|
||||
assertThat( actualEntry.getValue(), is( expectedEntry.getValue() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Search")
|
||||
@Table(name = "Search")
|
||||
public static class Search {
|
||||
|
||||
@Id
|
||||
private String searchString;
|
||||
|
||||
@ElementCollection
|
||||
@SortNatural
|
||||
private SortedSet<String> searchResults = new TreeSet<>();
|
||||
|
||||
@ElementCollection
|
||||
@SortComparator(StringCaseInsensitiveComparator.class)
|
||||
private SortedSet<String> searchResultsCaseInsensitive = new TreeSet<>();
|
||||
|
||||
@ElementCollection
|
||||
@SortNatural
|
||||
private SortedMap<String, String> tokens = new TreeMap<>();
|
||||
|
||||
@ElementCollection
|
||||
@SortComparator(StringCaseInsensitiveComparator.class)
|
||||
private SortedMap<String, String> tokensCaseInsensitive = new TreeMap<>();
|
||||
|
||||
public Search() {
|
||||
}
|
||||
|
||||
public Search(String searchString) {
|
||||
this.searchString = searchString;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package org.hibernate.orm.test.sorted;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Serves as a common {@link Comparator<T>} util class to test {@link org.hibernate.annotations.SortComparator SortComparator}
|
||||
* annotation. Basically a {@link String#CASE_INSENSITIVE_ORDER} wrapper because {@code @SortComparator}
|
||||
* annotation only supports {@code class} value property, not {@code class} instance.
|
||||
*
|
||||
* @author Nathan Xu
|
||||
* @see org.hibernate.annotations.SortComparator
|
||||
*/
|
||||
public class StringCaseInsensitiveComparator implements Comparator<String> {
|
||||
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
return String.CASE_INSENSITIVE_ORDER.compare( s1, s2 );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package org.hibernate.orm.test.sorted.map;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.MapKey;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
import org.hibernate.orm.test.sorted.StringCaseInsensitiveComparator;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@SuppressWarnings( "unused" )
|
||||
public class SortComparatorTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] { Owner.class, Cat.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortComparator() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Owner owner = new Owner();
|
||||
Cat cat1 = new Cat();
|
||||
Cat cat2 = new Cat();
|
||||
cat1.owner = owner;
|
||||
cat1.name = "B";
|
||||
cat1.nickname = "B";
|
||||
cat2.owner = owner;
|
||||
cat2.name = "a";
|
||||
cat2.nickname = "a";
|
||||
owner.cats.put( cat1.nickname, cat1 );
|
||||
owner.cats.put( cat2.nickname, cat2 );
|
||||
s.persist( owner );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.beginTransaction();
|
||||
|
||||
owner = s.get( Owner.class, owner.id );
|
||||
assertNotNull( owner.cats );
|
||||
assertEquals( 2, owner.cats.size() );
|
||||
|
||||
Iterator<Map.Entry<String, Cat>> entryIterator = owner.cats.entrySet().iterator();
|
||||
Map.Entry<String, Cat> firstEntry = entryIterator.next();
|
||||
Map.Entry<String, Cat> secondEntry = entryIterator.next();
|
||||
|
||||
assertEquals( "a", firstEntry.getKey() );
|
||||
assertEquals( "a", firstEntry.getValue().nickname );
|
||||
assertEquals( "B", secondEntry.getKey() );
|
||||
assertEquals( "B", secondEntry.getValue().nickname );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity(name = "Owner")
|
||||
@Table(name = "Owner")
|
||||
private static class Owner {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
|
||||
@MapKey(name = "nickname")
|
||||
@SortComparator( StringCaseInsensitiveComparator.class )
|
||||
private SortedMap<String, Cat> cats = new TreeMap<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Cat")
|
||||
@Table(name = "Cat")
|
||||
private static class Cat implements Comparable<Cat> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
private Owner owner;
|
||||
|
||||
private String name;
|
||||
|
||||
private String nickname;
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat other) {
|
||||
return this.name.compareTo( other.name );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package org.hibernate.orm.test.sorted.map;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.MapKey;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@SuppressWarnings( "unused" )
|
||||
public class SortNaturalTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] { Owner.class, Cat.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortNatural() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Owner owner = new Owner();
|
||||
Cat cat1 = new Cat();
|
||||
Cat cat2 = new Cat();
|
||||
cat1.owner = owner;
|
||||
cat1.name = "B";
|
||||
cat2.owner = owner;
|
||||
cat2.name = "A";
|
||||
owner.cats.put( cat1.name, cat1 );
|
||||
owner.cats.put( cat2.name, cat2 );
|
||||
s.persist( owner );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.beginTransaction();
|
||||
|
||||
owner = s.get( Owner.class, owner.id );
|
||||
assertNotNull(owner.cats);
|
||||
assertEquals( 2, owner.cats.size() );
|
||||
|
||||
Iterator<Map.Entry<String, Cat>> entryIterator = owner.cats.entrySet().iterator();
|
||||
Map.Entry<String, Cat> firstEntry = entryIterator.next();
|
||||
Map.Entry<String, Cat> secondEntry = entryIterator.next();
|
||||
|
||||
assertEquals( "A", firstEntry.getKey() );
|
||||
assertEquals( "A", firstEntry.getValue().name );
|
||||
assertEquals( "B", secondEntry.getKey() );
|
||||
assertEquals( "B", secondEntry.getValue().name );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity(name = "Owner")
|
||||
@Table(name = "Owner")
|
||||
private static class Owner {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
|
||||
@MapKey(name = "name")
|
||||
@SortNatural
|
||||
private SortedMap<String, Cat> cats = new TreeMap<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Cat")
|
||||
@Table(name = "Cat")
|
||||
private static class Cat implements Comparable<Cat> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
private Owner owner;
|
||||
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat other) {
|
||||
return this.name.compareTo( other.name );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package org.hibernate.orm.test.sorted.set;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@SuppressWarnings( "unused" )
|
||||
public class SortComparatorTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] { Owner.class, Cat.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortComparator() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Owner owner = new Owner();
|
||||
Cat cat1 = new Cat();
|
||||
Cat cat2 = new Cat();
|
||||
cat1.owner = owner;
|
||||
cat1.name = "B";
|
||||
cat1.nickname = "B";
|
||||
cat2.owner = owner;
|
||||
cat2.name = "a";
|
||||
cat2.nickname = "a";
|
||||
owner.cats.add( cat1 );
|
||||
owner.cats.add( cat2 );
|
||||
s.persist( owner );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.beginTransaction();
|
||||
|
||||
owner = s.get( Owner.class, owner.id );
|
||||
|
||||
assertNotNull(owner.cats);
|
||||
assertEquals( 2, owner.cats.size() );
|
||||
assertEquals( "a", owner.cats.first().nickname );
|
||||
assertEquals( "B", owner.cats.last().nickname );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity(name = "Owner")
|
||||
@Table(name = "Owner")
|
||||
private static class Owner {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
|
||||
@SortComparator( CatNicknameComparator.class )
|
||||
private SortedSet<Cat> cats = new TreeSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Cat")
|
||||
@Table(name = "Cat")
|
||||
private static class Cat implements Comparable<Cat> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
private Owner owner;
|
||||
|
||||
private String name;
|
||||
|
||||
private String nickname;
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat other) {
|
||||
return this.name.compareTo( other.name );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CatNicknameComparator implements Comparator<Cat> {
|
||||
|
||||
@Override
|
||||
public int compare(Cat cat1, Cat cat2) {
|
||||
return String.CASE_INSENSITIVE_ORDER.compare( cat1.nickname, cat2.nickname );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package org.hibernate.orm.test.sorted.set;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@SuppressWarnings( "unused" )
|
||||
public class SortNaturalTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] { Owner.class, Cat.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-8827" )
|
||||
public void testSortNatural() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Owner owner = new Owner();
|
||||
Cat cat1 = new Cat();
|
||||
Cat cat2 = new Cat();
|
||||
cat1.owner = owner;
|
||||
cat1.name = "B";
|
||||
cat2.owner = owner;
|
||||
cat2.name = "A";
|
||||
owner.cats.add( cat1 );
|
||||
owner.cats.add( cat2 );
|
||||
s.persist( owner );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.beginTransaction();
|
||||
|
||||
owner = s.get( Owner.class, owner.id );
|
||||
assertNotNull( owner.cats );
|
||||
assertEquals( 2, owner.cats.size() );
|
||||
assertEquals( "A", owner.cats.first().name );
|
||||
assertEquals( "B", owner.cats.last().name );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity(name = "Owner")
|
||||
@Table(name = "Owner")
|
||||
private static class Owner {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
|
||||
@SortNatural
|
||||
private SortedSet<Cat> cats = new TreeSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Cat")
|
||||
@Table(name = "Cat")
|
||||
private static class Cat implements Comparable<Cat> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
private Owner owner;
|
||||
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat other) {
|
||||
return this.name.compareTo( other.name );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -52,7 +52,7 @@ public class OneToOneWithDerivedIdentityTest extends SessionFactoryBasedFunction
|
|||
inTransaction(
|
||||
session -> {
|
||||
Person person = session.get( Person.class, PERSON_ID );
|
||||
assertEquals( person.getName(), "Alfio" );
|
||||
assertEquals( "Alfio", person.getName() );
|
||||
} );
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase {
|
|||
t.commit();
|
||||
s.close();
|
||||
|
||||
assertEquals( passp.getVersion(), 1 );
|
||||
assertEquals( 1, passp.getVersion() );
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
<?xml version="1.0"?>
|
||||
<!--
|
||||
~ 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>.
|
||||
-->
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<!--
|
||||
|
||||
-->
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.sorted">
|
||||
|
||||
<class name="Search">
|
||||
<id name="searchString"/>
|
||||
<set name="searchResults" sort="natural">
|
||||
<key column="searchString"/>
|
||||
<element column="text" type="string"/>
|
||||
</set>
|
||||
<map name="tokens" sort="natural" table="search_tokens">
|
||||
<key column="searchString"/>
|
||||
<map-key column="`key`" type="string"/>
|
||||
<element column="`value`" type="string"/>
|
||||
</map>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* 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>.
|
||||
*/
|
||||
|
||||
//$Id: Search.java 7772 2005-08-05 23:03:46Z oneovthafew $
|
||||
package org.hibernate.test.sorted;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Search {
|
||||
private String searchString;
|
||||
private SortedSet searchResults = new TreeSet();
|
||||
private SortedMap<String,String> tokens = new TreeMap<>();
|
||||
|
||||
Search() {}
|
||||
|
||||
public Search(String string) {
|
||||
searchString = string;
|
||||
}
|
||||
|
||||
public String getSearchString() {
|
||||
return searchString;
|
||||
}
|
||||
|
||||
public void setSearchString(String searchString) {
|
||||
this.searchString = searchString;
|
||||
}
|
||||
|
||||
public SortedSet getSearchResults() {
|
||||
return searchResults;
|
||||
}
|
||||
|
||||
public void setSearchResults(SortedSet searchResults) {
|
||||
this.searchResults = searchResults;
|
||||
}
|
||||
|
||||
public SortedMap<String, String> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public void setTokens(SortedMap<String, String> tokens) {
|
||||
this.tokens = tokens;
|
||||
}
|
||||
}
|
|
@ -1,193 +0,0 @@
|
|||
/*
|
||||
* 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.sorted;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
import org.hibernate.mapping.Collection;
|
||||
import org.hibernate.mapping.PersistentClass;
|
||||
import org.hibernate.mapping.Property;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class SortTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "sorted/Search.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] { Owner.class, Cat.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortedSetDefinitionInHbmXml() {
|
||||
final PersistentClass entityMapping = metadata().getEntityBinding( Search.class.getName() );
|
||||
|
||||
final Property sortedSetProperty = entityMapping.getProperty( "searchResults" );
|
||||
final Collection sortedSetMapping = assertTyping( Collection.class, sortedSetProperty.getValue() );
|
||||
assertTrue( "SortedSet mapping not interpreted as sortable", sortedSetMapping.isSorted() );
|
||||
|
||||
final Property sortedMapProperty = entityMapping.getProperty( "tokens" );
|
||||
final Collection sortedMapMapping = assertTyping( Collection.class, sortedMapProperty.getValue() );
|
||||
assertTrue( "SortedMap mapping not interpreted as sortable", sortedMapMapping.isSorted() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
public void testOrderBy() {
|
||||
Search search = new Search("Hibernate");
|
||||
search.getSearchResults().add("jboss.com");
|
||||
search.getSearchResults().add("hibernate.org");
|
||||
search.getSearchResults().add("HiA");
|
||||
|
||||
inTransaction(
|
||||
sess -> {
|
||||
sess.persist(search);
|
||||
sess.flush();
|
||||
|
||||
sess.clear();
|
||||
CriteriaBuilder criteriaBuilder = sess.getCriteriaBuilder();
|
||||
CriteriaQuery<Search> criteria = criteriaBuilder.createQuery( Search.class );
|
||||
criteria.from( Search.class );
|
||||
Search s = sess.createQuery( criteria ).uniqueResult();
|
||||
// Search s = (Search) sess.createCriteria(Search.class).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( s.getSearchResults() ) );
|
||||
Iterator iter = s.getSearchResults().iterator();
|
||||
assertEquals( iter.next(), "HiA" );
|
||||
assertEquals( iter.next(), "hibernate.org" );
|
||||
assertEquals( iter.next(), "jboss.com" );
|
||||
assertFalse( iter.hasNext() );
|
||||
|
||||
sess.clear();
|
||||
criteria = criteriaBuilder.createQuery( Search.class );
|
||||
Root<Search> root = criteria.from( Search.class );
|
||||
root.fetch( "searchResults" , JoinType.LEFT);
|
||||
s = sess.createQuery( criteria ).uniqueResult();
|
||||
|
||||
// s = (Search) sess.createCriteria(Search.class)
|
||||
// .setFetchMode("searchResults", FetchMode.JOIN)
|
||||
// .uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( s.getSearchResults() ) );
|
||||
iter = s.getSearchResults().iterator();
|
||||
assertEquals( iter.next(), "HiA" );
|
||||
assertEquals( iter.next(), "hibernate.org" );
|
||||
assertEquals( iter.next(), "jboss.com" );
|
||||
assertFalse( iter.hasNext() );
|
||||
|
||||
sess.clear();
|
||||
s = (Search) sess.createQuery("from Search s left join fetch s.searchResults")
|
||||
.uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( s.getSearchResults() ) );
|
||||
iter = s.getSearchResults().iterator();
|
||||
assertEquals( iter.next(), "HiA" );
|
||||
assertEquals( iter.next(), "hibernate.org" );
|
||||
assertEquals( iter.next(), "jboss.com" );
|
||||
assertFalse( iter.hasNext() );
|
||||
|
||||
sess.delete(s);
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-8827")
|
||||
public void testSortNatural() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Owner owner = new Owner();
|
||||
Cat cat1 = new Cat();
|
||||
Cat cat2 = new Cat();
|
||||
cat1.owner = owner;
|
||||
cat1.name = "B";
|
||||
cat2.owner = owner;
|
||||
cat2.name = "A";
|
||||
owner.cats.add( cat1 );
|
||||
owner.cats.add( cat2 );
|
||||
s.persist( owner );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.clear();
|
||||
|
||||
s.beginTransaction();
|
||||
|
||||
owner = (Owner) s.get( Owner.class, owner.id );
|
||||
assertNotNull(owner.cats);
|
||||
assertEquals(owner.cats.size(), 2);
|
||||
assertEquals(owner.cats.first().name, "A");
|
||||
assertEquals(owner.cats.last().name, "B");
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "Owner")
|
||||
private static class Owner {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
|
||||
@SortNatural
|
||||
private SortedSet<Cat> cats = new TreeSet<Cat>();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "Cat")
|
||||
private static class Cat implements Comparable<Cat> {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
private Owner owner;
|
||||
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat cat) {
|
||||
return this.name.compareTo( cat.name );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ import javax.persistence.MapKeyEnumerated;
|
|||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.hibernate.annotations.OrderBy;
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
|
||||
/**
|
||||
|
@ -34,23 +35,25 @@ public class EntityOfMaps {
|
|||
private Integer id;
|
||||
private String name;
|
||||
|
||||
private Map<String,String> basicByBasic;
|
||||
private SortedMap<String, String> sortedBasicByBasic;
|
||||
private Map<String, String> basicByBasic;
|
||||
|
||||
private Map<EnumValue,String> basicByEnum;
|
||||
private Map<EnumValue,String> basicByConvertedEnum;
|
||||
private Map<EnumValue, String> basicByEnum;
|
||||
private Map<EnumValue, String> basicByConvertedEnum;
|
||||
|
||||
private Map<String, SimpleComponent> componentByBasic;
|
||||
private Map<SimpleComponent,String> basicByComponent;
|
||||
private Map<SimpleComponent, String> basicByComponent;
|
||||
|
||||
private Map<String, SimpleEntity> oneToManyByBasic;
|
||||
private Map<SimpleEntity,String> basicByOneToMany;
|
||||
private Map<SimpleEntity, String> basicByOneToMany;
|
||||
|
||||
private Map<String, SimpleEntity> manyToManyByBasic;
|
||||
private SortedMap<String, SimpleEntity> sortedManyToManyByBasic;
|
||||
|
||||
private Map<String, SimpleComponent> componentByBasicOrdered;
|
||||
|
||||
private SortedMap<String, String> sortedBasicByBasic;
|
||||
private SortedMap<String, String> sortedBasicByBasicWithComparator;
|
||||
private SortedMap<String, SimpleEntity> sortedManyToManyByBasic;
|
||||
private SortedMap<String, SimpleEntity> sortedManyToManyByBasicWithComparator;
|
||||
|
||||
public EntityOfMaps() {
|
||||
}
|
||||
|
||||
|
@ -116,6 +119,26 @@ public class EntityOfMaps {
|
|||
sortedBasicByBasic.put( key, val );
|
||||
}
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// sortedBasicByBasicWithComparator
|
||||
|
||||
@ElementCollection
|
||||
@SortComparator( SimpleBasicSortComparator.class )
|
||||
public SortedMap<String, String> getSortedBasicByBasicWithComparator() {
|
||||
return sortedBasicByBasicWithComparator;
|
||||
}
|
||||
|
||||
public void setSortedBasicByBasicWithComparator(SortedMap<String, String> sortedBasicByBasicWithComparator) {
|
||||
this.sortedBasicByBasicWithComparator = sortedBasicByBasicWithComparator;
|
||||
}
|
||||
|
||||
public void addSortedBasicByBasicWithComparator(String key, String val) {
|
||||
if ( sortedBasicByBasicWithComparator == null ) {
|
||||
sortedBasicByBasicWithComparator = new TreeMap<>();
|
||||
}
|
||||
sortedBasicByBasicWithComparator.put( key, val );
|
||||
}
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// basicByEnum
|
||||
|
||||
|
@ -271,6 +294,19 @@ public class EntityOfMaps {
|
|||
this.sortedManyToManyByBasic = sortedManyToManyByBasic;
|
||||
}
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// sortedManyToManyByBasicWithComparator
|
||||
|
||||
@ManyToMany
|
||||
@SortComparator( SimpleBasicSortComparator.class )
|
||||
public SortedMap<String, SimpleEntity> getSortedManyToManyByBasicWithComparator() {
|
||||
return sortedManyToManyByBasicWithComparator;
|
||||
}
|
||||
|
||||
public void setSortedManyToManyByBasicWithComparator(SortedMap<String, SimpleEntity> sortedManyToManyByBasicWithComparator) {
|
||||
this.sortedManyToManyByBasicWithComparator = sortedManyToManyByBasicWithComparator;
|
||||
}
|
||||
|
||||
public void addSortedManyToManyByComponent(String key, SimpleEntity value) {
|
||||
if ( sortedManyToManyByBasic == null ) {
|
||||
sortedManyToManyByBasic = new TreeMap<>();
|
||||
|
|
|
@ -24,6 +24,7 @@ import javax.persistence.Table;
|
|||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
|
||||
/**
|
||||
|
@ -38,6 +39,8 @@ public class EntityOfSets {
|
|||
|
||||
private Set<String> setOfBasics;
|
||||
private SortedSet<String> sortedSetOfBasics;
|
||||
private SortedSet<String> sortedSetOfBasicsWithComparator;
|
||||
|
||||
private Set<String> orderedSetOfBasics;
|
||||
|
||||
private Set<EnumValue> setOfEnums;
|
||||
|
@ -140,6 +143,26 @@ public class EntityOfSets {
|
|||
sortedSetOfBasics.add( value );
|
||||
}
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// sortedSetOfBasicsWithComparator
|
||||
|
||||
@ElementCollection()
|
||||
@CollectionTable( name = "EntityOfSet_sortedBasicsWithComparator")
|
||||
@SortComparator( SimpleBasicSortComparator.class )
|
||||
public SortedSet<String> getSortedSetOfBasicsWithComparator() {
|
||||
return sortedSetOfBasicsWithComparator;
|
||||
}
|
||||
|
||||
public void setSortedSetOfBasicsWithComparator(SortedSet<String> sortedSetOfBasicsWithComparator) {
|
||||
this.sortedSetOfBasicsWithComparator = sortedSetOfBasicsWithComparator;
|
||||
}
|
||||
|
||||
public void addSortedBasicWithComparator(String value) {
|
||||
if ( sortedSetOfBasicsWithComparator == null ) {
|
||||
sortedSetOfBasicsWithComparator = new TreeSet<>();
|
||||
}
|
||||
sortedSetOfBasicsWithComparator.add( value );
|
||||
}
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// setOfConvertedEnums
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* 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.testing.orm.domain.gambit;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
public class SimpleBasicSortComparator implements Comparator<String> {
|
||||
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
return String.CASE_INSENSITIVE_ORDER.compare( s1, s2 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue