Skip to content

Commit

Permalink
HHH-7630 allow joined subclass ordering to explicitly reference natural
Browse files Browse the repository at this point in the history
  • Loading branch information
brmeyer committed Dec 11, 2013
1 parent 8e2c9c7 commit 4627f7f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
Expand Up @@ -39,16 +39,14 @@

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;
}
Expand Down
Expand Up @@ -1037,6 +1037,14 @@ public Declarer getSubclassPropertyDeclarer(String propertyPath) {

@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( "\"" )
Expand Down
Expand Up @@ -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 };
Expand All @@ -46,4 +53,39 @@ public void testQuerySyntaxCheck() {
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() );
}
}
Expand Up @@ -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" )
Expand Down Expand Up @@ -73,7 +74,7 @@ public void setCity(String city) {
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() {
Expand All @@ -84,7 +85,7 @@ public void setTigers(Set<Tiger> tigers) {
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() {
Expand All @@ -94,4 +95,15 @@ public Set<Lion> getLions() {
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;
}
}

0 comments on commit 4627f7f

Please sign in to comment.