Pinaki, Thank you for looking at this. Here you are:
First, here's the modified method. (I printed out qDef in three different places) public List<E> findByExample( @NotNull E exampleInstance, @Nullable Attribute<E,?>[] excludeProperty, SingularAttribute<E,?>... orderFields ) throws DAORuntimeException { EntityManager entityManager = JpaUtil.getEntityManager(); OpenJPACriteriaBuilder builder = (OpenJPACriteriaBuilder) entityManager.getCriteriaBuilder(); CriteriaQuery<E> qDef = builder.createQuery(persistentClass); System.err.println("qDef a = " + qDef); Root<E> from = qDef.from(persistentClass); qDef.select(from); qDef.where(builder.qbe(from, exampleInstance, excludeProperty)); System.err.println("qDef b = " + qDef); List<Order> orderList = makeOrderList(builder, qDef.from(persistentClass), orderFields); if (!orderList.isEmpty()) { qDef.orderBy(orderList); } System.err.println("qDef c = " + qDef); Query query = entityManager.createQuery(qDef); return (List<E>) query.getResultList(); } I ran it twice. The first time was with no order specified, where it worked correctly. The second time was with an order. Here are the first correct results: qDef a = SELECT * FROM qDef b = SELECT c FROM company c WHERE c.name = 'ACME' qDef c = SELECT c FROM company c, company c WHERE c.name = 'ACME' 2993 jobhunt TRACE [main] openjpa.jdbc.SQL - <t 1337300467, conn 868537867> executing prepstmnt 1444598836 SELECT t0.id, t0.fax, t0.name, t0.phone, t0.recruiter, t0.website FROM company t0 WHERE (t0.name = ?) [params=(String) ACME] 2994 jobhunt TRACE [main] openjpa.jdbc.SQL - <t 1337300467, conn 868537867> [1 ms] spent Found 1 companies. id 1: ACME at 555-1212 Here are the second incorrect results: qDef a = SELECT * FROM qDef b = SELECT c FROM company c WHERE c.name = 'ACME' qDef c = SELECT c FROM company c, company c WHERE c.name = 'ACME' ORDER BY c.name 2997 jobhunt TRACE [main] openjpa.jdbc.SQL - <t 1337300467, conn 868537867> executing prepstmnt 1049496898 SELECT t0.id, t0.fax, t0.name, t0.phone, t0.recruiter, t0.website, t1.name FROM company t0 CROSS JOIN company t1 WHERE (t0.name = ?) ORDER BY t1.name ASC [params=(String) ACME] 2998 jobhunt TRACE [main] openjpa.jdbc.SQL - <t 1337300467, conn 868537867> [1 ms] spent Found 4 companies. id 1: ACME at 555-1212 id 1: ACME at 555-1212 id 1: ACME at 555-1212 id 1: ACME at 555-1212 The qDef values look right. I don't know why it added a join to the actual SQL. On May 30, 2013, at 10:54 AM, Pinaki Poddar wrote: > please post > 1. the criteria query i.e. the output of System.err.println(qDef); > it should print a JPQL-like string > > 2. the SQL generated at execution with bind parameters. OpenJPA will print > the SQL if configured as follows > <property name="openjpa.Log" value="SQL=TRACE"> > <property name="openjpa.ConnectionFactoryProperties" > value="PrintParameters=true"/> > > > > > > > ----- > Pinaki Poddar > Chair, Apache OpenJPA Project > -- > View this message in context: > http://openjpa.208410.n2.nabble.com/Ordering-results-obtained-through-query-by-example-tp7584043p7584046.html > Sent from the OpenJPA Users mailing list archive at Nabble.com. ------------------------------------------- Miguel Muñoz swingguy1...@yahoo.com 323/225-7285 ------------------------------------------- The Sun, with all those planets revolving around it and dependent on it, can still ripen a vine of grapes like it had nothing else to do in the world. -- Galileo ------------------------------------------- There are seven sins in the world. Wealth without work. Pleasure without conscience. Knowledge without character. Commerce without morality. Science without humanity. Worship without sacrifice. Politics without principle. -- Mohandas Gandhi ------------------------------------------- If tyranny and oppression come to this land, it will come in the guise of fighting a foreign enemy. -- James Madison