HHH-10774 - SortedMap defined in .hbm.xml are not sortable

This commit is contained in:
Steve Ebersole 2016-06-19 12:08:20 -05:00
parent b112e5af3f
commit 60aac117e6
4 changed files with 77 additions and 10 deletions

View File

@ -11,9 +11,15 @@ import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmMapType;
import org.hibernate.boot.model.source.spi.AttributeSourceContainer;
import org.hibernate.boot.model.source.spi.PluralAttributeIndexSource;
import org.hibernate.boot.model.source.spi.PluralAttributeNature;
import org.hibernate.boot.model.source.spi.Sortable;
import org.hibernate.internal.util.StringHelper;
public class PluralAttributeSourceMapImpl extends AbstractPluralAttributeSourceImpl implements IndexedPluralAttributeSource {
public class PluralAttributeSourceMapImpl
extends AbstractPluralAttributeSourceImpl
implements IndexedPluralAttributeSource, Sortable {
private final String sorting;
private final PluralAttributeIndexSource indexSource;
private final String xmlNodeName;
public PluralAttributeSourceMapImpl(
@ -23,6 +29,8 @@ public class PluralAttributeSourceMapImpl extends AbstractPluralAttributeSourceI
super( sourceMappingDocument, jaxbMap, container );
this.xmlNodeName = jaxbMap.getNode();
this.sorting = interpretSorting( jaxbMap.getSort() );
if ( jaxbMap.getMapKey() != null ) {
this.indexSource = new PluralAttributeMapKeySourceBasicImpl( sourceMappingDocument, jaxbMap.getMapKey() );
}
@ -69,6 +77,18 @@ public class PluralAttributeSourceMapImpl extends AbstractPluralAttributeSourceI
}
}
private static String interpretSorting(String sort) {
if ( StringHelper.isEmpty( sort ) ) {
return null;
}
if ( "unsorted".equals( sort ) ) {
return null;
}
return sort;
}
@Override
public PluralAttributeIndexSource getIndexSource() {
return indexSource;
@ -88,4 +108,14 @@ public class PluralAttributeSourceMapImpl extends AbstractPluralAttributeSourceI
public String getXmlNodeName() {
return xmlNodeName;
}
@Override
public boolean isSorted() {
return sorting != null;
}
@Override
public String getComparatorName() {
return sorting;
}
}

View File

@ -21,6 +21,11 @@
<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>

View File

@ -7,12 +7,15 @@
//$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() {}
@ -20,16 +23,27 @@ public class Search {
searchString = string;
}
public SortedSet getSearchResults() {
return searchResults;
}
public void setSearchResults(SortedSet searchResults) {
this.searchResults = searchResults;
}
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;
}
}

View File

@ -22,11 +22,16 @@ import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
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.BaseCoreFunctionalTestCase;
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;
@ -36,7 +41,7 @@ import static org.junit.Assert.assertTrue;
* @author Gavin King
* @author Brett Meyer
*/
public class SortTest extends BaseCoreFunctionalTestCase {
public class SortTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
@ -48,6 +53,19 @@ public class SortTest extends BaseCoreFunctionalTestCase {
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() {