gigz
gigz

Reputation: 942

Oracle/J2EE Hibernate : ORA-00001: unique constraint violated due to HBM generated sequence

I am always getting contraint error

Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint violated

whenever i am trying to call a synchronized method which inserts a value in a table whose Primary Key is generated by HBM:

 <id name="logId" type="java.lang.Long">
        <column name="LOG_ID" precision="20" scale="0" />
        <generator class="sequence">
            <param name="sequence">TRANS_LOG_ID</param>
            <param name="allocationSize">100</param>
        </generator>
    </id>

this is the method called by the thread:

public synchronized static void saveTransLog(String detail, String stage) {

        TransLog transLog = new TransLog();
        transLog.setDetail(detail);
        transLog.setStage(stage);

    ...

        TransLogService.save(transLog);

}

I want to know if is there a much more reliable lock handling for these kinds of multithreaded scenarios. Or is there something i needed to add in order to fix my constraint violations.

Thank you very much.

Upvotes: 0

Views: 3043

Answers (1)

Muhammad Kashif Nazar
Muhammad Kashif Nazar

Reputation: 23825

May be the current value of the sequence is conflicting with the primary key of your row. You can verify this by using the following query.

SELECT trans_log_id.curr_val FROM dual;

And comparing this number with the maximum value of the column LOG_ID.

These two values should be either equal or the current value of the sequence should be greater. If the current value of the sequence is lesser, use the statement trans_log_id.next_val to increment the value of the sequence.

Upvotes: 1

Related Questions