aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How do you get underlying sql exceptions when using Java persistence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How do you get underlying sql exceptions when using Java persistence" Watch "How do you get underlying sql exceptions when using Java persistence" New topic
Author

How do you get underlying sql exceptions when using Java persistence

Jag Bains
Greenhorn

Joined: Sep 08, 2005
Posts: 3
Hi,

I want to know if it is possible to get hold of the underlying sql exceptions when using enityManager to persist data.

In my example, I try to insert a duplicate row into the database, and I would like to receive a duplicate row exception.

Instead I get returned a javax.persistence.PersistenceException and embeded in the text is the sql exception :

Caught an exception javax.persistence.PersistenceException: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '123' for key 2
Error Code: 1062

Is there anyway I can get the error code so that I can deal with the exception in the appropriate way.

My code is shown below:


Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do you get underlying sql exceptions when using Java persistence