捕获org.hibernate.exception.ConstraintViolationException

时间:2014-02-12 07:29:42

标签: java hibernate exception

将一些对象保存到数据库中

  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 {
        }
    }

我仍然得到了异常跟踪org.hibernate.exception.ConstraintViolationException 我也试过了

catch(Exception e){}

但仍然在输出中打印了迹线

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

当我试图抓住

java.sql.BatchUpdateException 

我得到“此异常从未在此try语句中抛出”

任何想法如何摆脱这个stackTrace?

0 个答案:

没有答案