developers

    Forum Stats

  • 3,874,042 Users
  • 2,266,667 Discussions
  • 7,911,702 Comments

Discussions

EclipseLink JPA: execution sequence and throwing unique constraint violation

user613332
user613332 Member Posts: 6 Blue Ribbon
edited Sep 11, 2022 11:23PM in Java User Groups Discussions

I am using EclipseLink 2.5.2 as JPA implementation. I created a UM_User entity and its user_id is generated by sequence of UM_USER_ID_SEQ. Here is portion of UM_User entity

@Entity

@Table(name="UM_USER")

public class UmUser implements Serializable {

      private static final long serialVersionUID = 1L;

  @Id

 @SequenceGenerator(name="seqGen", sequenceName="UM_USER_ID_SEQ", allocationSize=1)

      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqGen")

      @Column(name="USER_ID")

      private long userId;  

Here is the sequence of UM_USER_ID_SEQ in database (Oracle 19)


User_id in UM_User is defined as primary key as


When I run the following method,

public void createNewUser(String id) throws Exception {

 

            EntityManager em = getEntityManager();

            EntityTransaction tx = em.getTransaction();

            System.out.println("Start createNewUser");

       try {

           tx.begin(); 

    UmUser user = new UmUser();

user.setJId(id);

           user.setStatus("N");     

           user.setLoginCount(new BigDecimal(0));

           em.flush();

                   em.persist(user);               

                   tx.commit();

       } catch(Exception e) {

           if(tx.isActive()) {

                  tx.rollback();

           }

           e.printStackTrace();

       } finally {

           em.close();

       }

       System.out.println("End createNewUser");

      }

I get an exception as

javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (XXXX.UM_USER_PK) violated


Error Code: 1

Call: INSERT INTO UM_USER (USER_ID,J_ID,STATUS, CREATED_BY, CREATED_DATE, DIVISION, EMAIL, LOGIN_COUNT, FIRST_NAME, ACCESS_TIMESTAMP, LAST_NAME, PHONE_NUMBER) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

      bind => [0, Test0001,N, null, null, null, null, null, 0, null, null, null]

I am wondering why user_id value is 0. It is supposed to be 35 as it shows in the UM_USER_ID_SEQ sequence. It looks like the sequence id not working, Please help me to identify the issue and thank you very much.

Answers

  • user613332
    user613332 Member Posts: 6 Blue Ribbon

    I have new findings that no matter if I insert UM_User entity or update UM_User entity to database, UM_USER_ID_SEQ always gets called as

    SELECT XXXX.UM_USER_ID_SEQ.NEXTVAL FROM DUAL

    ALTER XXXX,SEQUENCE UM_USER_ID_SEQ INCREMENT BY 1

    However, if I do insert, I get  unique constraint (XXXX.UM_USER_PK) violated exception and user_id field is set to 0 even UM_USER_ID_SEQ gets called. Did I do something wrong? Thanks for your help.

developers