HHH-7116 test case and fix

This commit is contained in:
Tommy Knowlton 2012-06-19 22:11:19 -06:00 committed by Brett Meyer
parent 4ab29b4811
commit 87004f9815
4 changed files with 149 additions and 1 deletions

View File

@ -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) {

View File

@ -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;
}

View File

@ -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<Object> results = criteria.list();
tx.rollback();
s.close();
}
}

View File

@ -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<Bid> bids = new HashSet<Bid>();
}