HHH-6875 - Fix and test

This commit is contained in:
Lukasz Antoniak 2013-04-15 21:08:23 +02:00 committed by Brett Meyer
parent 2bc63badea
commit 51b9248c4b
3 changed files with 78 additions and 7 deletions

View File

@ -1389,12 +1389,9 @@ public abstract class CollectionBinder {
private String extractHqlOrderBy(javax.persistence.OrderBy jpaOrderBy) { private String extractHqlOrderBy(javax.persistence.OrderBy jpaOrderBy) {
if ( jpaOrderBy != null ) { if ( jpaOrderBy != null ) {
final String jpaOrderByFragment = jpaOrderBy.value(); return jpaOrderBy.value(); // Null not possible. In case of empty expression, apply default ordering.
return StringHelper.isNotEmpty( jpaOrderByFragment )
? jpaOrderByFragment
: null;
} }
return null; return null; // @OrderBy not found.
} }
private static void checkFilterConditions(Collection collValue) { private static void checkFilterConditions(Collection collValue) {

View File

@ -23,14 +23,21 @@
*/ */
package org.hibernate.test.annotations.collectionelement.ordered; package org.hibernate.test.annotations.collectionelement.ordered;
import org.hibernate.Session; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.persister.collection.BasicCollectionPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
public class ElementCollectionSortingTest extends BaseCoreFunctionalTestCase { public class ElementCollectionSortingTest extends BaseCoreFunctionalTestCase {
@Override @Override
@ -54,4 +61,71 @@ public class ElementCollectionSortingTest extends BaseCoreFunctionalTestCase {
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
} }
@Test
@TestForIssue( jiraKey = "HHH-6875" )
public void testSortingEmbeddableCollectionOfPrimitives() {
final Session session = openSession();
session.beginTransaction();
final Person steve = new Person();
steve.setName( "Steve" );
steve.getNickNamesAscendingNaturalSort().add( "sebersole" );
steve.getNickNamesAscendingNaturalSort().add( "ebersole" );
steve.getNickNamesDescendingNaturalSort().add( "ebersole" );
steve.getNickNamesDescendingNaturalSort().add( "sebersole" );
final Person lukasz = new Person();
lukasz.setName( "Lukasz" );
lukasz.getNickNamesAscendingNaturalSort().add( "antoniak" );
lukasz.getNickNamesAscendingNaturalSort().add( "lantoniak" );
lukasz.getNickNamesDescendingNaturalSort().add( "lantoniak" );
lukasz.getNickNamesDescendingNaturalSort().add( "antoniak" );
session.save( steve );
session.save( lukasz );
session.flush();
session.clear();
final List<String> lukaszNamesAsc = Arrays.asList( "antoniak", "lantoniak" );
final List<String> lukaszNamesDesc = Arrays.asList( "lantoniak", "antoniak" );
final List<String> steveNamesAsc = Arrays.asList( "ebersole", "sebersole" );
final List<String> steveNamesDesc = Arrays.asList( "sebersole", "ebersole" );
// Testing object graph navigation. Lazy loading collections.
checkPersonNickNames( lukaszNamesAsc, lukaszNamesDesc, (Person) session.get( Person.class, lukasz.getId() ) );
checkPersonNickNames( steveNamesAsc, steveNamesDesc, (Person) session.get( Person.class, steve.getId() ) );
session.clear();
// Testing HQL query. Eagerly fetching nicknames.
final List<Person> result = session.createQuery(
"select distinct p from Person p join fetch p.nickNamesAscendingNaturalSort join fetch p.nickNamesDescendingNaturalSort order by p.name"
).list();
Assert.assertEquals( 2, result.size() );
checkPersonNickNames( lukaszNamesAsc, lukaszNamesDesc, result.get( 0 ) );
checkPersonNickNames( steveNamesAsc, steveNamesDesc, result.get( 1 ) );
// Metadata verification.
checkSQLOrderBy( session, Person.class.getName(), "nickNamesAscendingNaturalSort", "asc" );
checkSQLOrderBy( session, Person.class.getName(), "nickNamesDescendingNaturalSort", "desc" );
session.getTransaction().rollback();
session.close();
}
private void checkSQLOrderBy(Session session, String entityName, String propertyName, String order) {
String roleName = entityName + "." + propertyName;
String alias = "alias1";
BasicCollectionPersister collectionPersister = (BasicCollectionPersister) session.getSessionFactory().getCollectionMetadata( roleName );
Assert.assertTrue( collectionPersister.hasOrdering() );
Assert.assertEquals( alias + "." + propertyName + " " + order, collectionPersister.getSQLOrderByString( alias ) );
}
private void checkPersonNickNames(List<String> expectedAscending, List<String> expectedDescending, Person person) {
// Comparing lists to verify ordering.
Assert.assertEquals( expectedAscending, new ArrayList<String>( person.getNickNamesAscendingNaturalSort() ) );
Assert.assertEquals( expectedDescending, new ArrayList<String>( person.getNickNamesDescendingNaturalSort() ) );
}
} }

View File

@ -76,7 +76,7 @@ public class Person {
@ElementCollection @ElementCollection
@JoinColumn @JoinColumn
@JoinTable(name = "T_NICKNAMES_A") @JoinTable(name = "T_NICKNAMES_A")
@OrderBy @OrderBy // testing default @OrderBy mapping
public Set<String> getNickNamesAscendingNaturalSort() { public Set<String> getNickNamesAscendingNaturalSort() {
return nickNamesAscendingNaturalSort; return nickNamesAscendingNaturalSort;
} }