Skip to Main Content

Java User Groups

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

EclipseLink JPA: execution sequence and throwing unique constraint violation

user613332Sep 7 2022 — edited Sep 11 2022

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)
image.png
User_id in UM_User is defined as primary key as
image.png
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.

Comments
Post Details
Added on Sep 7 2022
1 comment
989 views