HHH-7630 allow joined subclass ordering to explicitly reference natural

This commit is contained in:
Brett Meyer 2013-12-11 13:46:54 -05:00
parent a317dfd526
commit cac31981f0
4 changed files with 70 additions and 10 deletions

View File

@ -39,16 +39,14 @@ import org.hibernate.type.Type;
public final class ArrayHelper { public final class ArrayHelper {
/*public static boolean contains(Object[] array, Object object) { public static boolean contains(Object[] array, Object object) {
for ( int i=0; i<array.length; i++ ) { return indexOf( array, object ) > -1;
if ( array[i].equals(object) ) return true; }
}
return false;
}*/
public static int indexOf(Object[] array, Object object) { public static int indexOf(Object[] array, Object object) {
for ( int i=0; i<array.length; i++ ) { for ( int i = 0; i < array.length; i++ ) {
if ( array[i].equals(object) ) return i; if ( array[i].equals( object ) )
return i;
} }
return -1; return -1;
} }

View File

@ -836,6 +836,14 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
@Override @Override
public int determineTableNumberForColumn(String columnName) { public int determineTableNumberForColumn(String columnName) {
// HHH-7630: In case the naturalOrder/identifier column is explicitly given in the ordering, check here.
for ( int i = 0, max = naturalOrderTableKeyColumns.length; i < max; i++ ) {
final String[] keyColumns = naturalOrderTableKeyColumns[i];
if ( ArrayHelper.contains( keyColumns, columnName ) ) {
return naturalOrderPropertyTableNumbers[i];
}
}
final String[] subclassColumnNameClosure = getSubclassColumnClosure(); final String[] subclassColumnNameClosure = getSubclassColumnClosure();
for ( int i = 0, max = subclassColumnNameClosure.length; i < max; i++ ) { for ( int i = 0, max = subclassColumnNameClosure.length; i < max; i++ ) {
final boolean quoted = subclassColumnNameClosure[i].startsWith( "\"" ) final boolean quoted = subclassColumnNameClosure[i].startsWith( "\"" )

View File

@ -23,16 +23,23 @@
*/ */
package org.hibernate.test.collection.ordered.joinedInheritence; package org.hibernate.test.collection.ordered.joinedInheritence;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.hibernate.Session; import org.hibernate.Session;
import org.junit.Test; import org.junit.Test;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
* @author Brett Meyer
*/ */
public class OrderCollectionOfJoinedHierarchyTest extends BaseCoreFunctionalTestCase { public class OrderCollectionOfJoinedHierarchyTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Animal.class, Lion.class, Tiger.class, Zoo.class }; return new Class[] { Animal.class, Lion.class, Tiger.class, Zoo.class };
@ -46,4 +53,39 @@ public class OrderCollectionOfJoinedHierarchyTest extends BaseCoreFunctionalTest
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
} }
@Test
public void testOrdering() {
Zoo zoo = new Zoo();
Lion lion1 = new Lion();
lion1.setWeight( 2 );
Lion lion2 = new Lion();
lion2.setWeight( 1 );
zoo.getLions().add( lion1 );
zoo.getLions().add( lion2 );
zoo.getAnimalsById().add( lion1 );
zoo.getAnimalsById().add( lion2 );
Session session = openSession();
session.beginTransaction();
session.persist( lion1 );
session.persist( lion2 );
session.persist( zoo );
session.getTransaction().commit();
session.clear();
session.beginTransaction();
zoo = (Zoo) session.get( Zoo.class, zoo.getId() );
zoo.getLions().size();
zoo.getLions().size();
zoo.getAnimalsById().size();
session.getTransaction().commit();
session.close();
assertNotNull( zoo );
assertTrue( CollectionHelper.isNotEmpty( zoo.getLions() ) && zoo.getLions().size() == 2 );
assertTrue( CollectionHelper.isNotEmpty( zoo.getAnimalsById() ) && zoo.getAnimalsById().size() == 2 );
assertEquals( zoo.getLions().iterator().next().getId(), lion2.getId() );
assertEquals( zoo.getAnimalsById().iterator().next().getId(), lion1.getId() );
}
} }

View File

@ -45,6 +45,7 @@ public class Zoo {
private String city; private String city;
private Set<Tiger> tigers = new HashSet<Tiger>(); private Set<Tiger> tigers = new HashSet<Tiger>();
private Set<Lion> lions = new HashSet<Lion>(); private Set<Lion> lions = new HashSet<Lion>();
private Set<Animal> animals = new HashSet<Animal>();
@Id @Id
@GeneratedValue( generator = "increment" ) @GeneratedValue( generator = "increment" )
@ -73,7 +74,7 @@ public class Zoo {
this.city = city; this.city = city;
} }
@OneToMany( fetch = FetchType.EAGER ) // eager so that load-by-id queries include this association @OneToMany
@JoinColumn @JoinColumn
@javax.persistence.OrderBy( "weight" ) @javax.persistence.OrderBy( "weight" )
public Set<Tiger> getTigers() { public Set<Tiger> getTigers() {
@ -84,7 +85,7 @@ public class Zoo {
this.tigers = tigers; this.tigers = tigers;
} }
@OneToMany( fetch = FetchType.EAGER ) // eager so that load-by-id queries include this association @OneToMany
@JoinColumn @JoinColumn
@org.hibernate.annotations.OrderBy( clause = "weight" ) @org.hibernate.annotations.OrderBy( clause = "weight" )
public Set<Lion> getLions() { public Set<Lion> getLions() {
@ -94,4 +95,15 @@ public class Zoo {
public void setLions(Set<Lion> lions) { public void setLions(Set<Lion> lions) {
this.lions = lions; this.lions = lions;
} }
@OneToMany
@JoinColumn
@javax.persistence.OrderBy( "id asc" ) // HHH-7630 ensure explicitly naming the superclass id works
public Set<Animal> getAnimalsById() {
return animals;
}
public void setAnimalsById(Set<Animal> animals) {
this.animals = animals;
}
} }