1 Reply Latest reply on Jul 24, 2008 11:24 AM by peterj

    ERROR: duplicate key violates unique constraint

    vivekmk2006

      Hi all,

      I am trying to execute the bellow code,

      private boolean isUserNameExist(){
      Users users = getInstance();
      String qry = "from Users where userName=:userName AND tenantId =:tenantId";
      Query query = entityManager.createQuery(qry);
      query.setParameter("userName",users.getUserName()).
      setParameter("tenantId", users.getTenantId());
      List lst =query.getResultList();

      return lst.size() > 0 ? true : false;
      }


      but i am getting an entirely different exception like bellow.The exception is throwing at "List lst =query.getResultList(); " .

      11:22:54,328 ERROR [JDBCExceptionReporter] ERROR: duplicate key violates unique constraint "ad_users_u2"
      11:22:54,328 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      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)


      Can anybody can help me to fix it.

      Thanks in advance
      vivek

        • 1. Re: ERROR: duplicate key violates unique constraint
          peterj

          Are you sure that you posted the correct code? I ask because the error message you posted is typically seen during an update, and not a query, and the code you posted relates to a query. (You really should have posted enough of the stack trace so that we could see a line that included the method in your code that initiated this.)

          If it is this code, then the database is already corrupt. That is, the database already contains a duplicate key. In which case you will have to fix this using database tools.