openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Miguel Muñoz <SwingGuy1...@yahoo.com>
Subject Re: Ordering results obtained through query-by-example
Date Thu, 30 May 2013 10:27:07 GMT
John,

  Good point, but it turns out it doesn't make any difference. I had actually written the
code that way. I moved the createQuery() call up to see if that helped, and forgot to move
it back down before writing my email. Both ways produce the same result.

— Miguel



On May 30, 2013, at 2:48 AM, Boblitz John wrote:

> Just an idea, but maybe the 
> 
> 	Query query = entityManager.createQuery(qDef);
> 
> Should come after this:
> 
> 	List<Order> orderList = makeOrderList(builder, qDef.from(persistentClass), orderFields);
> 		if (!orderList.isEmpty()) {
> 			qDef.orderBy(orderList);
> 		}
> 
> ?
> 
> Cheers, 
> 
> John
> 
> ---- 
> 
> Who is General Failure, and why is he reading my hard disk?
> 
> 
> 
> 
>> -----Original Message-----
>> From: Miguel Muñoz [mailto:SwingGuy1024@yahoo.com]
>> Sent: Thursday, May 30, 2013 11:08 AM
>> To: users@openjpa.apache.org
>> Subject: Ordering results obtained through query-by-example
>> 
>> Comrades,
>> 
>>  I'm using the OpenJPA query-by-example API, and it works fine unless I
>> order my results. When I do that, I get way too many results. If I search
>> without specifying an order, I get 4 results from a table of 9 rows. If I specify
>> an order, I get 36 results instead. (I get everything four times.) The number I
>> get is always the number I'm supposed to get multiplied by the number of
>> rows.
>> 
>> Has anybody else seen this behavior? I'm trying to figure out if I'm doing
>> something wrong, or if it's a bug in the JPA code.
>> 
>> Here's my method, if you're interested:
>> 
>> 	private final Class<E> persistentClass;
>> 
>> 	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);
>> 		Root<E> from = qDef.from(persistentClass);
>> 		qDef.select(from);
>> 		qDef.where(builder.qbe(from, exampleInstance,
>> excludeProperty)); // query by example
>> 		Query query = entityManager.createQuery(qDef);
>> 
>> 		List<Order> orderList = makeOrderList(builder,
>> qDef.from(persistentClass), orderFields);
>> 		if (!orderList.isEmpty()) {
>> 			qDef.orderBy(orderList);
>> 		}
>> 
>> 		return (List<E>) query.getResultList();
>> 	}
>> 
>> 	private List<Order> makeOrderList(
>> 			CriteriaBuilder builder,
>> 			Root<E> root,
>> 			SingularAttribute<E, ?>[] pOrderFields
>> 	) {
>> 		List<Order> orderList = new LinkedList<>();
>> 		for (SingularAttribute<E, ?> attribute: pOrderFields) {
>> 			orderList.add(builder.asc(root.get(attribute)));
>> 		}
>> 		return orderList;
>> 	}
>> 
>> 
>> If I call it like this, it works fine:
>> 
>> 		List<Incident> dIncidentList =
>> incidentDao.findByExample(dExample, null);
>> 
>> If I call it like this, it returns lots of duplicates:
>> 
>> 		List<Incident> dIncidentList =
>> incidentDao.findByExample(dExample, null, Incident_.entryTime);
>> 
>> Is it me? Has anyone else seen this bug?
>> 
>> 
>> -------------------------------------------
>> 
>> Miguel Muñoz
>> SwingGuy1024@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
>> 
>> 
> 

-------------------------------------------

Miguel Muñoz
SwingGuy1024@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




Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message