HHH-7116 Moved orderBy fix to AbstractEntityJoinWalker. Copyrights,

formatting, and checkstyle
This commit is contained in:
Brett Meyer 2013-07-01 14:03:51 -04:00
parent 87004f9815
commit 17d9a2c1fc
5 changed files with 113 additions and 83 deletions

View File

@ -205,6 +205,16 @@ public abstract class AbstractEntityJoinWalker extends JoinWalker {
public final String getAlias() { public final String getAlias() {
return alias; return alias;
} }
/**
* For entities, orderings added by, for example, Criteria#addOrder need to come before the associations' @OrderBy
* values. However, other sub-classes of JoinWalker (BasicCollectionJoinWalker, OneToManyJoinWalker, etc.)
* still need the other way around. So, override here instead. See HHH-7116.
*/
@Override
protected String orderBy(final List associations, final String orderBy) {
return mergeOrderings( orderBy, orderBy( associations ) );
}
public String toString() { public String toString() {
return getClass().getName() + '(' + getPersister().getEntityName() + ')'; return getClass().getName() + '(' + getPersister().getEntityName() + ')';

View File

@ -847,7 +847,7 @@ public class JoinWalker {
} }
protected String orderBy(final List associations, final String orderBy) { protected String orderBy(final List associations, final String orderBy) {
return mergeOrderings( orderBy, orderBy( associations ) ); return mergeOrderings( orderBy( associations ), orderBy );
} }
protected static String mergeOrderings(String ordering1, String ordering2) { protected static String mergeOrderings(String ordering1, String ordering2) {

View File

@ -7,14 +7,13 @@ import java.io.Serializable;
/** /**
* @author tknowlton at iamhisfriend dot org * @author tknowlton at iamhisfriend dot org
* @since 5/21/12 12:45 AM
*/ */
@Entity @Entity
public class Bid implements Serializable { public class Bid implements Serializable {
@Id @Id
float amount; float amount;
@Id @Id
@ManyToOne @ManyToOne
Item item; Item item;
} }

View File

@ -1,3 +1,23 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.test.criteria; package org.hibernate.test.criteria;
import org.hibernate.Criteria; import org.hibernate.Criteria;
@ -15,94 +35,96 @@ import java.util.List;
/** /**
* @author tknowlton at iamhisfriend dot org * @author tknowlton at iamhisfriend dot org
* @since 5/20/12 10:50 PM
*/ */
public class CriteriaOrderByTest extends BaseCoreFunctionalTestCase { public class CriteriaOrderByTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class[]{ return new Class[] { Bid.class, Item.class };
Bid.class, Item.class }
};
}
@Test @Test
@TestForIssue(jiraKey = "HHH-7116") @TestForIssue(jiraKey = "HHH-7116")
public void testCriteriaOrderBy() { public void testCriteriaOrderBy() {
Session s = openSession(); final Session s = openSession();
Transaction tx = s.beginTransaction(); final Transaction tx = s.beginTransaction();
Item item; Item item;
Bid bid; Bid bid;
item = new Item(); item = new Item();
item.name = "ZZZZ"; item.name = "ZZZZ";
s.persist(item); s.persist( item );
bid = new Bid(); bid = new Bid();
bid.amount = 444.44f; bid.amount = 444.44f;
bid.item = item; bid.item = item;
s.persist(bid); s.persist( bid );
item = new Item(); item = new Item();
item.name = "AAAA"; item.name = "AAAA";
s.persist(item); s.persist( item );
bid = new Bid(); bid = new Bid();
bid.amount = 222.22f; bid.amount = 222.22f;
bid.item = item; bid.item = item;
s.persist(bid); s.persist( bid );
item = new Item(); item = new Item();
item.name = "MMMM"; item.name = "MMMM";
s.persist(item); s.persist( item );
bid = new Bid(); bid = new Bid();
bid.amount = 999.99f; bid.amount = 999.99f;
bid.item = item; bid.item = item;
s.persist(bid); s.persist( bid );
s.flush(); s.flush();
// For each item, ordered by name, show all bids made by bidders on this item. // 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. // 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. // For some reason, the association mappings' ordering specifications are not honored if default (INNER) join
Criteria criteria = s.createCriteria(Item.class) // type is used.
.addOrder(org.hibernate.criterion.Order.asc("this.name")) final Criteria criteria = s
.createAlias("this.bids", "i_bid", JoinType.LEFT_OUTER_JOIN) .createCriteria( Item.class )
.setProjection(Projections.projectionList() .addOrder( org.hibernate.criterion.Order.asc( "this.name" ) )
.add(Projections.property("this.name"), "item_name") .createAlias( "this.bids", "i_bid", JoinType.LEFT_OUTER_JOIN )
.add(Projections.property("i_bid.amount"), "bid_amount")) .setProjection(
.setResultTransformer(new ResultTransformer() { Projections.projectionList().add( Projections.property( "this.name" ), "item_name" )
boolean first = true; .add( Projections.property( "i_bid.amount" ), "bid_amount" ) )
Object[] previous; .setResultTransformer( new ResultTransformer() {
boolean first = true;
Object[] previous;
@Override @Override
public Object transformTuple(Object[] tuple, String[] aliases) { public Object transformTuple(Object[] tuple, String[] aliases) {
if (first) { if ( first ) {
first = false; first = false;
previous = tuple; previous = tuple;
} else { }
String previousName = (String) previous[0]; else {
String name = (String) tuple[0]; final String previousName = (String) previous[0];
final 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); Assert.assertTrue(
"The resultset tuples should be ordered by item name, as specified on the Criteria",
previousName.compareTo( name ) < 1 );
previous = tuple; previous = tuple;
} }
return tuple; return tuple;
} }
@Override @Override
public List transformList(List collection) { public List transformList(List collection) {
return collection; return collection;
} }
}); } );
List<Object> results = criteria.list(); criteria.list();
tx.rollback(); tx.rollback();
s.close(); s.close();
} }
} }

View File

@ -7,14 +7,13 @@ import java.util.Set;
/** /**
* @author tknowlton at iamhisfriend dot org * @author tknowlton at iamhisfriend dot org
* @since 5/21/12 12:38 AM
*/ */
@Entity @Entity
public class Item implements Serializable { public class Item implements Serializable {
@Id @Id
String name; String name;
@OneToMany(mappedBy = "item", fetch = FetchType.EAGER) @OneToMany(mappedBy = "item", fetch = FetchType.EAGER)
@OrderBy("amount desc") @OrderBy("amount desc")
Set<Bid> bids = new HashSet<Bid>(); Set<Bid> bids = new HashSet<Bid>();
} }