We want to have the option to add staffnumbers manual in the application.
You can set the Id in the application.
When i set an Id that already exists i get a ConstraintViolationException. I'm trying to catch it but it doesn't work, because i still get it.
What is the best way to handle this problem. It's Hibernate database communication.


Java Code:
   public static boolean insertStaff(Staff s){
        boolean result = false;

        try {
            Session session = NewHibernateUtil.getSessionFactory().openSession();
            Transaction transaction  = session.beginTransaction();
            transaction.begin();
            session.save(s);
            transaction.commit();
            result = true;
        } catch (ConstraintViolationException  ex) {
            System.out.println(ex.getMessage() + ex.getClass() );
            return false;            
        }
        return result;
    }
Error:
SEVERE: Duplicate entry '2' for key 'PRIMARY'
5-jun-2011 19:36:26 org.hibernate.event.def.AbstractFlushingEventListe ner performExecutions
Could not execute JDBC batch updateclass org.hibernate.exception.ConstraintViolationExcepti on
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationExcepti on: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r.java:298)
at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:1000)
at org.hibernate.impl.SessionImpl.managedFlush(Sessio nImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(J DBCTransaction.java:106)
at Database.DatabaseConnectie.insertStaff(DatabaseCon nectie.java:663)
at Controller.Controller.AddStaff(Controller.java:405 )