diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/ArrayHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/ArrayHelper.java index 807a26d860..8a928d2642 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/ArrayHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/ArrayHelper.java @@ -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 -1; + } public static int indexOf(Object[] array, Object object) { - for ( int i=0; i[] 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() ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java b/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java index 2fc48baea4..8aa0d18439 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java @@ -45,6 +45,7 @@ public class Zoo { private String city; private Set tigers = new HashSet(); private Set lions = new HashSet(); + private Set animals = new HashSet(); @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 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 getLions() { @@ -94,4 +95,15 @@ public class Zoo { public void setLions(Set lions) { this.lions = lions; } + + @OneToMany + @JoinColumn + @javax.persistence.OrderBy( "id asc" ) // HHH-7630 ensure explicitly naming the superclass id works + public Set getAnimalsById() { + return animals; + } + + public void setAnimalsById(Set animals) { + this.animals = animals; + } }