1

I have an oracle table with a unique contraint on a few fields. I am writing a piece of code to import data to that table from large excel files (thousands of rows usually). The whole import has to be in one transaction.

It might happen that in that thousands of rows there will be one or two rows duplicated. So I need to ignore SQLIntegrityConstraintViolationException and continue inserting all other rows. I just need to show message to the user that the rowNo 123 is a duplicate entry.

I am reading the excel file row by row (Apache POI event based), and each row is maped to hibernate POJO and saved right away:

public void saveRow( ExcelRow row) {
  TableRow tableRow = new TableRow(row);
  //some logic goes here
  this.session.save(tableRow);
}

I tried to suround session.save with

try{
    session.save(tableRow);
}catch(SQLIntegrityConstraintViolationException e)
{
    //push the error message and continue
}

But Netbeans cries that exception SQLIntegrityConstraintViolationException is never thrown in body of corresponding try statement.

How to catch and ignore the integrity exception to continue processing data?

I would rather not have to execute another 10K selects to check if similar row exists.

1
  • I believe the SQLIntegrityConstraintViolationException is wrapped in some other exception. Look for a org.hibernate.exception.ConstraintViolationException Commented May 23, 2013 at 13:33

1 Answer 1

1

Hibernate wraps SQL exceptions within its own exception stack, usually a org.hibernate.HibernateException, which is a RuntimeException. You can either catch that or if you know that it will be a constraint violation, try to catch a ConstraintViolationException.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.