1

Am saving some objects into a database

  public boolean importPart(Part p) {
        System.err.println("THIS FUNCTION DOESN'T ClOSE THE SESSION");
        try {
            tx = s.beginTransaction();
            //s.saveOrUpdate(p);
            s.save(p);
            tx.commit();
            System.out.println("Operation completed");
            return true;

        } catch (org.hibernate.exception.ConstraintViolationException e) {
            System.out.println("Part exists in the database");
            if (tx != null) {
                tx.rollback();
            }
            return false;

        } finally {
        }
    }

am still getting the exceptiontrace org.hibernate.exception.ConstraintViolationException I tried also

catch(Exception e){}

but still the trace is printed in the output

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at javaapplication4.Importer.importPart(Importer.java:58)
    at javaapplication4.Importer.importFromFile(Importer.java:131)
    at javaapplication4.MyProject.main(MyProject.java:31)
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (SASSYSTEM.PART_U01) violated

and when I try to catch

java.sql.BatchUpdateException 

I get "this exception is never thrown in this try statement"

any ideas how can I get rid of this stackTrace ?

Exorcismus
  • 2,243
  • 1
  • 35
  • 68
  • By the way, testing the unique constraints of a database through exceptions is a slightly abuse of language constructs - I would check if a row with the given unique constraint already exists and using this result for further processing. – Smutje Feb 12 '14 at 07:34
  • 1
    actually that's what am doing but instead of writing a new code to check if an object exists... am taking advantage of hibernate exception because it checks them for me ... – Exorcismus Feb 12 '14 at 07:39
  • 1
    This approach is feasible, but not good language design, because exceptions are intended to separated normal and abnormal behavior not interceptable normally - and it makes your code much less readable than something like if (recordWithUniqueConstraintExists(recordToCreate)) { } else { } – Smutje Feb 12 '14 at 07:42
  • 1
    the database am dealing has millions of records ,so I can't search on row basis – Exorcismus Feb 12 '14 at 07:52
  • 1
    The database has to deal with the same problem when you blindly insert a duplicated and therefore violated unique constraint. If you have an index on the unique column, something like `SELECT COUNT(UNIQUE_COLUMN) FROM TABLE WHERE UNIQUE_COLUMN = 'UNIQUE_VALUE_TO_INSERT'` should process fast. – Smutje Feb 12 '14 at 08:08

0 Answers0