Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you get underlying sql exceptions when using Java persistence

Jag Bains
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic