aspose file tools*
The moose likes Spring and the fly likes How does Spring handle find(PrimaryKey pk) when an entity is not found ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How does Spring handle find(PrimaryKey pk) when an entity is not found ? " Watch "How does Spring handle find(PrimaryKey pk) when an entity is not found ? " New topic
Author

How does Spring handle find(PrimaryKey pk) when an entity is not found ?

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

HI,

I have a small problem regarding the way Spring handles JPA exceptions
Here is code snippet of my generic DAO :



And here is a snippet code of a DAO which inherits the generic DAO :


And here is the business class in which i inject the TotoDaoJpa DAO which inherits the generic DAO :


The problem is that i do not know which exception to catch when the find(...) method does not find the entity for the totoId parameter.
An exception (does not show in the logs) is thrown but it is either caught or translated by Spring because whenever i try to use the toto variable in a class
in the upper layer i get a ClassCastException : java.lang.String cannot be cast to Toto ...

Whcih exception is thrown by Spring when an entity is not found ?
EmptyResultDataAccessException ?
How do you handle that ?

Thanks.

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

The problem is that i do not know which exception to catch when the find(...) method does not find the entity for the totoId parameter.


I think there is some confusion about what Spring does in this case. When a Hibernate(or whatever provider you are using ) Exception or an SQLException gets thrown, Spring will translate the exception to a runtime exception within the DataAccessException hierarchy. entityManager.find() does not throw an exception if the entity is not found it returns null. As a matter of fact the only exception that em.find should throw is an IllegalArgumentException if the first argument does not denote an entity type or the second argument is is not a valid type for that entity’s primary key or is null.

If an exception is occurring and is being swallowed somehow, I would catch generic Exception e in your methodFoo() and log the stack trace. Then you should be able to determine the problem based on that. The ClassCastException you are referencing says you are trying to cast a String to a Toto object which of course is illegal. I have not looked closely at all the code you posted, mainly because it is a more productive use of my time to have the full stack trace first.

Good Luck


[How To Ask Questions][Read before you PM me]
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

Bill Gorder wrote:

If an exception is occurring and is being swallowed somehow, I would catch generic Exception e in your methodFoo() and log the stack trace. Then you should be able to determine the problem based on that. The ClassCastException you are referencing says you are trying to cast a String to a Toto object which of course is illegal. I have not looked closely at all the code you posted, mainly because it is a more productive use of my time to have the full stack trace first.

Good Luck


Yeah, that's what i planned to do but i haven't had the time yet.
Thanks for your answer, i will investigate further.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does Spring handle find(PrimaryKey pk) when an entity is not found ?