HHH-7630 allow joined subclass ordering to explicitly reference natural
This commit is contained in:
parent
8e2c9c7faf
commit
4627f7f489
|
@ -39,16 +39,14 @@ import org.hibernate.type.Type;
|
|||
|
||||
public final class ArrayHelper {
|
||||
|
||||
/*public static boolean contains(Object[] array, Object object) {
|
||||
for ( int i=0; i<array.length; i++ ) {
|
||||
if ( array[i].equals(object) ) return true;
|
||||
}
|
||||
return false;
|
||||
}*/
|
||||
public static boolean contains(Object[] array, Object object) {
|
||||
return indexOf( array, object ) > -1;
|
||||
}
|
||||
|
||||
public static int indexOf(Object[] array, Object object) {
|
||||
for ( int i=0; i<array.length; i++ ) {
|
||||
if ( array[i].equals(object) ) return i;
|
||||
for ( int i = 0; i < array.length; i++ ) {
|
||||
if ( array[i].equals( object ) )
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -1037,6 +1037,14 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
|
|||
|
||||
@Override
|
||||
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();
|
||||
for ( int i = 0, max = subclassColumnNameClosure.length; i < max; i++ ) {
|
||||
final boolean quoted = subclassColumnNameClosure[i].startsWith( "\"" )
|
||||
|
|
|
@ -23,16 +23,23 @@
|
|||
*/
|
||||
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.junit.Test;
|
||||
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
* @author Brett Meyer
|
||||
*/
|
||||
public class OrderCollectionOfJoinedHierarchyTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { Animal.class, Lion.class, Tiger.class, Zoo.class };
|
||||
|
@ -46,4 +53,39 @@ public class OrderCollectionOfJoinedHierarchyTest extends BaseCoreFunctionalTest
|
|||
session.getTransaction().commit();
|
||||
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() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,7 @@ public class Zoo {
|
|||
private String city;
|
||||
private Set<Tiger> tigers = new HashSet<Tiger>();
|
||||
private Set<Lion> lions = new HashSet<Lion>();
|
||||
private Set<Animal> animals = new HashSet<Animal>();
|
||||
|
||||
@Id
|
||||
@GeneratedValue( generator = "increment" )
|
||||
|
@ -73,7 +74,7 @@ public class Zoo {
|
|||
this.city = city;
|
||||
}
|
||||
|
||||
@OneToMany( fetch = FetchType.EAGER ) // eager so that load-by-id queries include this association
|
||||
@OneToMany
|
||||
@JoinColumn
|
||||
@javax.persistence.OrderBy( "weight" )
|
||||
public Set<Tiger> getTigers() {
|
||||
|
@ -84,7 +85,7 @@ public class Zoo {
|
|||
this.tigers = tigers;
|
||||
}
|
||||
|
||||
@OneToMany( fetch = FetchType.EAGER ) // eager so that load-by-id queries include this association
|
||||
@OneToMany
|
||||
@JoinColumn
|
||||
@org.hibernate.annotations.OrderBy( clause = "weight" )
|
||||
public Set<Lion> getLions() {
|
||||
|
@ -94,4 +95,15 @@ public class Zoo {
|
|||
public void setLions(Set<Lion> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue