From 87004f9815eeffad635d00ed25eda7894cb26b6d Mon Sep 17 00:00:00 2001 From: Tommy Knowlton Date: Tue, 19 Jun 2012 22:11:19 -0600 Subject: [PATCH] HHH-7116 test case and fix --- .../java/org/hibernate/loader/JoinWalker.java | 2 +- .../java/org/hibernate/test/criteria/Bid.java | 20 ++++ .../test/criteria/CriteriaOrderByTest.java | 108 ++++++++++++++++++ .../org/hibernate/test/criteria/Item.java | 20 ++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/criteria/Bid.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaOrderByTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/criteria/Item.java diff --git a/hibernate-core/src/main/java/org/hibernate/loader/JoinWalker.java b/hibernate-core/src/main/java/org/hibernate/loader/JoinWalker.java index affb387682..c05f4db8e8 100755 --- a/hibernate-core/src/main/java/org/hibernate/loader/JoinWalker.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/JoinWalker.java @@ -847,7 +847,7 @@ public class JoinWalker { } protected String orderBy(final List associations, final String orderBy) { - return mergeOrderings( orderBy( associations ), orderBy ); + return mergeOrderings( orderBy, orderBy( associations ) ); } protected static String mergeOrderings(String ordering1, String ordering2) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/criteria/Bid.java b/hibernate-core/src/test/java/org/hibernate/test/criteria/Bid.java new file mode 100644 index 0000000000..e6927fc279 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/criteria/Bid.java @@ -0,0 +1,20 @@ +package org.hibernate.test.criteria; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import java.io.Serializable; + +/** + * @author tknowlton at iamhisfriend dot org + * @since 5/21/12 12:45 AM + */ +@Entity +public class Bid implements Serializable { + @Id + float amount; + + @Id + @ManyToOne + Item item; +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaOrderByTest.java b/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaOrderByTest.java new file mode 100644 index 0000000000..265bde7f3a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaOrderByTest.java @@ -0,0 +1,108 @@ +package org.hibernate.test.criteria; + +import org.hibernate.Criteria; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.criterion.Projections; +import org.hibernate.sql.JoinType; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.transform.ResultTransformer; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + * @author tknowlton at iamhisfriend dot org + * @since 5/20/12 10:50 PM + */ +public class CriteriaOrderByTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Bid.class, Item.class + }; + } + + @Test + @TestForIssue(jiraKey = "HHH-7116") + public void testCriteriaOrderBy() { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Item item; + Bid bid; + + item = new Item(); + item.name = "ZZZZ"; + s.persist(item); + + bid = new Bid(); + bid.amount = 444.44f; + bid.item = item; + s.persist(bid); + + item = new Item(); + item.name = "AAAA"; + s.persist(item); + + bid = new Bid(); + bid.amount = 222.22f; + bid.item = item; + s.persist(bid); + + item = new Item(); + item.name = "MMMM"; + s.persist(item); + + bid = new Bid(); + bid.amount = 999.99f; + bid.item = item; + s.persist(bid); + + s.flush(); + + // For each item, ordered by name, show all bids made by bidders on this item. + // The joined collections item.bids and bidder.bids have orderings specified on the mappings. + // For some reason, the association mappings' ordering specifications are not honored if default (INNER) join type is used. + Criteria criteria = s.createCriteria(Item.class) + .addOrder(org.hibernate.criterion.Order.asc("this.name")) + .createAlias("this.bids", "i_bid", JoinType.LEFT_OUTER_JOIN) + .setProjection(Projections.projectionList() + .add(Projections.property("this.name"), "item_name") + .add(Projections.property("i_bid.amount"), "bid_amount")) + .setResultTransformer(new ResultTransformer() { + boolean first = true; + Object[] previous; + + @Override + public Object transformTuple(Object[] tuple, String[] aliases) { + if (first) { + first = false; + previous = tuple; + } else { + String previousName = (String) previous[0]; + String name = (String) tuple[0]; + + Assert.assertTrue("The resultset tuples should be ordered by item name, as specified on the Criteria", previousName.compareTo(name) < 1); + + previous = tuple; + } + + return tuple; + } + + @Override + public List transformList(List collection) { + return collection; + } + }); + + List results = criteria.list(); + + tx.rollback(); + s.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/criteria/Item.java b/hibernate-core/src/test/java/org/hibernate/test/criteria/Item.java new file mode 100644 index 0000000000..d38c8f5f3b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/criteria/Item.java @@ -0,0 +1,20 @@ +package org.hibernate.test.criteria; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +/** + * @author tknowlton at iamhisfriend dot org + * @since 5/21/12 12:38 AM + */ +@Entity +public class Item implements Serializable { + @Id + String name; + + @OneToMany(mappedBy = "item", fetch = FetchType.EAGER) + @OrderBy("amount desc") + Set bids = new HashSet(); +}