Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA 2.0 BUG
icon8.gif  JPA 2.0 BUG [message #504097] Thu, 17 December 2009 09:06 Go to next message
No real name is currently offline No real nameFriend
Messages: 4
Registered: December 2009
Junior Member
Hi,

J2EE 6 JPA 2.0 API specifies that EntityManager's find(java.lang.Class<T> entityClass, java.lang.Object primaryKey) method must return null if the entity does not exist .

However EclipseLink's implementation throws java.lang.IllegalArgumentException even though both arguments are valid
Re: JPA 2.0 BUG [message #504102 is a reply to message #504097] Thu, 17 December 2009 09:24 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
> J2EE 6 JPA 2.0 API specifies that EntityManager's
> find(java.lang.Class<T> entityClass, java.lang.Object primaryKey) method
> must return null if the entity does not exist .
>
> However EclipseLink's implementation throws
> java.lang.IllegalArgumentException even though both arguments are valid

OH! Really? Interesting. I already wrote a utility method to convert the exception to null.
Re: JPA 2.0 BUG [message #504218 is a reply to message #504102] Thu, 17 December 2009 17:43 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
In my testing I get null back from find when the PK value(s) passed in are not found in the database.

I do get an IllegalArgumentException exception if the attribute type passed to find is not exactly the same as the type defined in the class.

Doug
Re: JPA 2.0 BUG [message #504253 is a reply to message #504218] Thu, 17 December 2009 21:42 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
> In my testing I get null back from find when the PK value(s) passed in
> are not found in the database.
> I do get an IllegalArgumentException exception if the attribute type
> passed to find is not exactly the same as the type defined in the class.

Well, I have to confirm Cristi; I replaced the getSingleResult calls with this utility method, using getResultList (throwing an exception is considered heavy, so I decided to go for the list):

/**
* getSingleResult throws an exception if nothing is found.
*
* @param lQuery
* @return
*/
static public Object getSingleResultOrNull(Query lQuery)
{
java.util.List lList = lQuery.getResultList();
if (lList.size() == 0) return null;
Object o = lList.get(0);
return o;
}

Naturally I can retest, but at the very least this was the case indeed.
Re: JPA 2.0 BUG [message #505724 is a reply to message #504253] Mon, 04 January 2010 16:35 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
I am a little confused. There seems to be two issues in this thread.

1. em.find will return null for a non existent entity assuming the identity type matches that of the mapped entity class.

2. query.getSingleResult() will thrown an exception if no entity is found executing the query.

If you have a case where em.find throws an IllegalArgumentException and the both the entity type and id type are valid then please log a bug with the example and we'll address it.

Cheers,

Doug
Re: JPA 2.0 BUG [message #505742 is a reply to message #505724] Mon, 04 January 2010 17:55 Go to previous message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
On 2010-01-04 17:35, Doug Clarke wrote:
> I am a little confused. There seems to be two issues in this thread.
>
> 1. em.find will return null for a non existent entity assuming the
> identity type matches that of the mapped entity class.
>
> 2. query.getSingleResult() will thrown an exception if no entity is
> found executing the query.
>

Oh my, you are so right. My static find methods used to use getSingleResult; so I was confused and mixed up my find with EM's find.

Tom
Previous Topic:NoSuchMethodException when using FetchType.Lazy and weaving
Next Topic:Workbench and Object-relational nested table mappings
Goto Forum:
  


Current Time: Fri Apr 19 20:13:35 GMT 2024

Powered by FUDForum. Page generated in 0.02724 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top