OPENJPA-1627: fix OrderBy with EmbeddedId.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@934101 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2010-04-14 18:37:30 +00:00
parent f90b0bb16f
commit 8a034d6717
3 changed files with 40 additions and 4 deletions

View File

@ -76,8 +76,34 @@ class JDBCRelatedFieldOrder
public void order(Select sel, ClassMapping elem, Joins joins) {
FieldMapping fm = _fm;
if (elem != null)
fm = elem.getFieldMapping(_fm.getIndex());
if (elem != null) {
fm = getOrderByField(elem, fm);
if (fm == null)
fm = elem.getFieldMapping(_fm.getIndex());
}
sel.orderBy(fm.getColumns(), _asc, joins, false);
}
private FieldMapping getOrderByField(ClassMapping elem, FieldMapping fm) {
ClassMapping owner = (ClassMapping)_fm.getDefiningMetaData();
if (owner.getDescribedType() == elem.getDescribedType())
return elem.getFieldMapping(_fm.getIndex());
else {
FieldMapping fms[] = elem.getFieldMappings();
for (int i = 0; i < fms.length; i++) {
ValueMapping vm = (ValueMapping)fms[i].getValue();
ClassMapping clm = (ClassMapping)vm.getEmbeddedMetaData();
if (clm != null) {
if (clm.getDescribedType() == owner.getDescribedType()) {
return owner.getFieldMapping(_fm.getIndex());
} else {
FieldMapping fm1 = getOrderByField(clm, fm);
if (fm1 != null)
return fm1;
}
}
}
}
return null;
}
}

View File

@ -24,8 +24,10 @@ import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import org.apache.openjpa.persistence.jdbc.VersionColumn;
@ -49,7 +51,8 @@ public class Library1 implements Serializable {
@Column(name="LIBRARY_NAME", nullable = false)
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "library")
@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "library")
@OrderBy(value = "bid.library ASC, bid.name ASC")
private Set<Book1> books = new HashSet<Book1>();
private String location;

View File

@ -200,7 +200,14 @@ public class TestMultipleLevelDerivedIdentity1 extends SQLListenerTestCase {
assertEquals(NUM_PAGES-1, count(Page1.class));
}
public void testOrderBy() {
sql.clear();
EntityManager em = emf.createEntityManager();
Library1 lib = em.find(Library1.class, LIBRARY_NAME);
assertNotNull(lib);
assertSQLFragnments(sql, "ORDER BY t1.LIBRARY_NAME ASC, t1.BOOK_NAME ASC");
}
/**
* Create a Library with a Book and three Pages.
*/