I am implementing
IdentifierGenerator interface for custom identifiers.
When I execute the main class at first time (database is clear, no objects, and execute DDL). The CreateID class generate IDs as well.
When I execute again the main class (without drop database tables and maintainning rows) the application raise up error saying duplicade keys were generated and tried to be commited.
But when I see all keys generated all keys are completally differents at runtime and at database, I cannot understand why app throws this exception.
Thanks all!
Code:
import java.io.Serializable;
import java.util.Random;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
/**
*
* @date 09/01/2008
* @since 1.0
* @version 1.0
* por InocĂȘncio T. de Oliveira
*/
public class CreateID implements IdentifierGenerator {
public Serializable generate(SessionImplementor pSession, Object pObj) throws HibernateException {
Serializable lPkValue = null;
Long lId = 0l;
String lTemp = "";
if (JPASyncUtil.getMode() == JPASyncUtil.DB_STATUS_DEFAULT) {
//temporariamente usada
lTemp = String.valueOf((Math.abs(new Random().nextInt())) / 1000) +
String.valueOf(new java.util.Date().getTime() / 100);
lId = Long.valueOf(lTemp);
lPkValue = lId;
}
return lPkValue;
}
}
Erro:Code:
Caused by: java.sql.SQLException: Violation of UNIQUE KEY constraint 'UQ__ADT_AUDITORIA__7D39791C'. Cannot insert duplicate key in object 'ADT_AUDITORIA'.