Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:29:31+00:00 2026-05-16T10:29:31+00:00

When a unique constraint is violated, a javax.persistence.RollbackException is thrown. But there could be

  • 0

When a unique constraint is violated, a javax.persistence.RollbackException is thrown. But there could be multiple reasons to throw a RollbackException. How can I find out that a unique constraint was violated?

try {
    repository.save(article);
}
catch(javax.persistence.RollbackException e) {
    // how to find out the reason for the rollback exception?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:29:31+00:00Added an answer on May 16, 2026 at 10:29 am

    How can I find out that a unique constraint was violated?

    Exception are chained, you have to call getCause() recursively to get the provider specific exception (and maybe go down to the SQLException) to translate it into something your application can handle nicely for your user. The following will print the chain of exception:

    for (t = e.getCause(); t != null; t = t.getCause()) {
        logger.debug("Exception:" + t);
    }
    

    For the exception handling and the “translation”, you could do something like what Spring does (see the various JpaDialect classes, e.g. HibernateJpaDialect to get an idea).

    All this is not nice, this code won’t be portable and finding what attribute(s) caused the violation won’t be easy. This somehow confirms that there is no elegant and portable way to handle constraint violations in JPA.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I basically carry out a unique constraint on a string data-type field.
We're getting a ORA-00001 (unique constraint violated) in a batch job. However, the error
Could someone clarify what is the purpose of having unique index without unique constraint
I try to drop unique constraint for column in h2, previously created as info
I am trying to create a unique constraint on two fields in a table.
I've been trying to get the UNIQUE constraint placed on some attributes I have
We're trying to add a unique constraint to our postgres table in a way
I'm using JPA 2.0 and want to create a unique constraint using XML, not
In SQL Server 2005+ (I use both), does adding the UNIQUE constraint to a
I'm trying to test Hibernate mappings, specifically a unique constraint. My POJO is mapped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.