Query returns no rows. In JPA would return empty List or Null

17,232 views
Skip to first unread message

Bytor99999

unread,
Aug 23, 2012, 7:10:00 PM8/23/12
to kundera...@googlegroups.com
Right now I am running a query that would return no results, and therefore the List<Persons> that I assign the results to should be an empty List. I believe that is how it works in JPA and probably in the Spec. But my results is that the List is being set to null, or just not assigned as it is begins null. So now I have to do a Null pointer check everytime I run a query?

Thanks

Mark

Vivek Mishra

unread,
Aug 23, 2012, 11:03:06 PM8/23/12
to kundera...@googlegroups.com
Well, as per jpa:

/**
* Execute a SELECT query and return the query results
* as an untyped List.
* @return a list of the results
* @throws IllegalStateException if called for a Java
*
Persistence query language UPDATE or DELETE statement
* @throws QueryTimeoutException if the query execution exceeds
*
the query timeout value set and only the statement is
*
rolled back
* @throws TransactionRequiredException if a lock mode has
*
been set and there is no transaction
* @throws PessimisticLockException if pessimistic locking
*
fails and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking
*
fails and only the statement is rolled back
* @throws PersistenceException if the query execution exceeds
*
the query timeout value set and the transaction
*
is rolled back
*/
List getResultList();



If there is no result, wondering what's the point to return an empty list?

-Vivek
________________________________________
From: kundera...@googlegroups.com [kundera...@googlegroups.com] on behalf of Bytor99999 [bytor...@gmail.com]
Sent: 24 August 2012 04:40
To: kundera...@googlegroups.com
Subject: {kundera-discuss} Query returns no rows. In JPA would return empty List or Null

Right now I am running a query that would return no results, and therefore the List<Persons> that I assign the results to should be an empty List. I believe that is how it works in JPA and probably in the Spec. But my results is that the List is being set to null, or just not assigned as it is begins null. So now I have to do a Null pointer check everytime I run a query?

Thanks

Mark

________________________________

Impetus Ranked in the Top 50 India’s Best Companies to Work For 2012.

Impetus webcast ‘Designing a Test Automation Framework for Multi-vendor Interoperable Systems’ available at http://lf1.me/0E/.


NOTE: This message may contain information that is confidential, proprietary, privileged or otherwise protected by law. The message is intended solely for the named addressee. If received in error, please destroy and notify the sender. Any use of this email is prohibited when received in error. Impetus does not represent, warrant and/or guarantee, that the integrity of this communication has been maintained nor that the communication is free of errors, virus, interception or interference.

Bytor99999

unread,
Aug 24, 2012, 2:45:39 AM8/24/12
to kundera...@googlegroups.com
Well a good reason to return an empty list would be that in some use cases you them loop through that list to do some business logic and the best way is the for each loop. But if you put the List into the for each you have to first check with an if to see if it is null. I am very much about the least amount of code and this would require 2 more lines of code for each use case that needs to loop. I actually think that that would be the majority where you loop through the list without checking for null. Even if it is a loop in a jsp page with jstl. If it could return null in that case you then have to even add a null check in the jsp page.

So while the api in JPA doesn't specify whether the implementation has to return an empty List or always return null. I am pretty positive that Hibernate's implementation of JPA returns an empty list.

You could say it is also like Lists in entities with @OneToMany If the One side doesn't have any children the List in the domain is not null, it has a List that is empty.

But again, I am not sure what the JPA spec says, if it says anything on the matter. If it says nothing then you can obviously do whatever you want. I would just document it in the javadocs that the resultList method might return null if no records are found. Or should it throw a NoResultsFoundException or whatever that exception class is.

Thanks for your reply.

Mark

Amresh

unread,
Aug 24, 2012, 2:50:46 AM8/24/12
to kundera...@googlegroups.com
That's a good point Mark, and although JPA doesn't clearly state required behavior, from usability point of view this makes sense and requires easy changes.

Regards,
Amresh

Vivek Mishra

unread,
Aug 24, 2012, 2:51:37 AM8/24/12
to kundera...@googlegroups.com
Hmm..i agree with you and Amresh.. returning empty or null both means "there is no record". But for better use of Kundera API i think returning an empty list should also be fine :)

-Vivek
________________________________________
From: kundera...@googlegroups.com [kundera...@googlegroups.com] on behalf of Bytor99999 [bytor...@gmail.com]
Sent: 24 August 2012 12:15
To: kundera...@googlegroups.com
Subject: {kundera-discuss} Re: Query returns no rows. In JPA would return empty List or Null
Reply all
Reply to author
Forward
0 new messages