fixes bug#7537
git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@56616 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
5c62c9ae06
commit
7ee26bbaf0
|
@ -0,0 +1,68 @@
|
|||
package com.rjconsultores.ventaboletos.dao.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.SessionImplementor;
|
||||
import org.hibernate.id.IdentifierGenerator;
|
||||
/**
|
||||
* Classe que trata a geração do ID da tabela Aidf.
|
||||
*
|
||||
* Foi abandonado o uso da sequence pois existiam lacunas nos numeros e já havia atingido o tamanho máximo de 3 dígitos, com isso
|
||||
* sendo necessário aproveitar os número que não haviam sido utilizados
|
||||
*
|
||||
* @author gleimar
|
||||
*
|
||||
*/
|
||||
public class AidfGenerator implements IdentifierGenerator {
|
||||
|
||||
private static Logger log = Logger.getLogger(AidfGenerator.class);
|
||||
|
||||
@Override
|
||||
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
|
||||
|
||||
int qtdMaxima = 999;
|
||||
long idEncontrado = -1;
|
||||
|
||||
Connection connection = session.connection();
|
||||
PreparedStatement ps;
|
||||
|
||||
int i=1;
|
||||
|
||||
try {
|
||||
|
||||
ps = connection.prepareStatement("select count(*) as qtd from aidf where aidf_id = :1");
|
||||
|
||||
for(; (idEncontrado == -1) && (i <= qtdMaxima);i++){
|
||||
ps.setInt(1, i);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
rs.next();
|
||||
int qtd = rs.getInt("qtd");
|
||||
|
||||
if (qtd == 0){
|
||||
idEncontrado = i;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
log.error("erro ao buscar id aidf",e);
|
||||
throw new HibernateException(e);
|
||||
}
|
||||
|
||||
|
||||
if (i == qtdMaxima){
|
||||
throw new HibernateException("A qtd de IDs permitidos para AIDF foi atingido:"+qtdMaxima);
|
||||
}
|
||||
|
||||
if (idEncontrado == -1){
|
||||
throw new HibernateException("Houve algum erro ao buscar um novo ID para AIDF");
|
||||
}
|
||||
return idEncontrado;
|
||||
}
|
||||
|
||||
}
|
|
@ -8,15 +8,15 @@ import javax.persistence.Column;
|
|||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.SequenceGenerator;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
/**
|
||||
* Aidf generated by hbm2java
|
||||
*/
|
||||
|
@ -24,6 +24,10 @@ import javax.persistence.TemporalType;
|
|||
@Table(name = "AIDF")
|
||||
public class Aidf implements java.io.Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Long aidfId;
|
||||
private AidfTipo aidfTipo;
|
||||
private AidfEspecie aidfEspecie;
|
||||
|
@ -93,9 +97,10 @@ public class Aidf implements java.io.Serializable {
|
|||
return true;
|
||||
}
|
||||
|
||||
@SequenceGenerator(name = "AIDF_SEQ", sequenceName = "AIDF_SEQ", allocationSize = 1)
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO, generator = "AIDF_SEQ")
|
||||
@GenericGenerator(name="aidfgen_id", strategy="com.rjconsultores.ventaboletos.dao.util.AidfGenerator")
|
||||
@GeneratedValue(generator="aidfgen_id")
|
||||
@Column(name = "AIDF_ID", unique = true, nullable = false, precision = 15, scale = 0)
|
||||
public Long getAidfId() {
|
||||
return aidfId;
|
||||
|
|
Loading…
Reference in New Issue