2

I have the following table named CONTACT in oracle database:

ID NOT NULL NUMBER(19) PRIMARY KEY,
CITY VARCHAR2(56),
EMAIL VARCHAR2(120),
FIRSTNAME VARCHAR2(40),
LASTNAME VARCHAR2(40)

The sequence name is CONTACT_ID_SEQ

Following is the mapping class:

@Entity
@Table(name ="CONTACT")
public class Contact implements Serializable{
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CONTACT_ID_SEQ")
  @SequenceGenerator(name = "CONTACT_ID_SEQ", sequenceName = "CONTACT_ID_SEQ")  
  private Long id;
 
  //All the other column mappings
  //Constructors
  //Getter setters

}

I am doing a batch inserts. Basically inserting a list of contacts. List size is at least 100. Following is the hibernate configuration:

<property name="show_sql">true</property>
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.cache.use_second_level_cache">false</property>

So the batch size is 20.

Following is the code for insert:

public void saveData(List<Contact> contacts){
  Session session = HibernateFactory.openSession();
  Transaction transaction = null;
  try{
        int i = 0;
        transaction = session.getTransaction() == null ? session.beginTransaction() : 
        session.getTransaction(); 
        for(Contact c : contacts){
            session.save(c);
            if(i > 0 && i % 20 == 0){
              log.info("Finished batch ");
              session.flush();
              session.clear();
            }
            i++;
        }
        transaction.commit();

  }catch(HibernateException e){
        transaction.rollback();
  }finally{
       session.close();
  }
}

I am getting the exception:

Error Uploading to Database: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute batch
.
.
.
.
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (CONTACT_DB.SYS_C0013605) violated

I am seeing the next val for the sequence being generated only once:

Hibernate: select CONTACT_ID_SEQ.nextval from dual

After the first batch(size 20) of inserts is complete. I presume it should generate the next val for the sequence once again.

I have been trying to address the constraint violation exception without any success. Any help would be appreciated. Thanks in advance!

VMA
  • 89
  • 1
  • 4
  • 16
  • What version of Oracle are you using? Can you [edit] your post, and add the appropriate version tag such as `Oracle12` -- if you have flexibility of recreating the table, and have version 12 or greater (I think), you can add the clause `GENERATED AS IDENTITY` before the `PRIMARY KEY` constraint, and Oracle will create a sequence and associate it with that table, and ensure it is up to date. – Mark Stewart Jun 30 '20 at 21:48
  • Have you seen the answer at https://stackoverflow.com/questions/52055666/hibernate-having-issues-with-sequence-object-unable-to-call-next-val ? – Mark Stewart Jun 30 '20 at 21:53

0 Answers0