• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

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

 
Ranch Hand
Posts: 560
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Celinio Fernandes
Ranch Hand
Posts: 560
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!